From c7e6c1f572c83f1fad74e299a3cbeb9712df4c90 Mon Sep 17 00:00:00 2001 From: Kolja Zuelsdorf Date: Fri, 7 Feb 2025 11:43:05 +0100 Subject: [PATCH] Added a debug tool to send API requests to WPcom via the Jetpack connection. (#41154) * Added a debug tool to send API requests to WPcom via the Jetpack connection. This is to help with testing singular API requests that are in development and supposed to be called by Jetpack over the connection. * Added changelog. * Ensured that the connection classes actually exist (so that a helper can be set up) before using this helper. Moved module initialization to static method, so we don't have an empty object to construct. * Update phan baseline. * Update phan baseline. * Don't add extra IP for public API. * Extended request faker to allow different HTTP methods other than GET. --- .../plugins/debug-helper/.phan/baseline.php | 1 + .../debug-add-wpcom-api-request-faker | 4 + .../class-wpcom-api-request-faker-module.php | 185 ++++++++++++++++++ projects/plugins/debug-helper/plugin.php | 5 + 4 files changed, 195 insertions(+) create mode 100644 projects/plugins/debug-helper/changelog/debug-add-wpcom-api-request-faker create mode 100644 projects/plugins/debug-helper/modules/class-wpcom-api-request-faker-module.php diff --git a/projects/plugins/debug-helper/.phan/baseline.php b/projects/plugins/debug-helper/.phan/baseline.php index b7c093aa21e3b..724eaf018ac4c 100644 --- a/projects/plugins/debug-helper/.phan/baseline.php +++ b/projects/plugins/debug-helper/.phan/baseline.php @@ -45,6 +45,7 @@ 'modules/class-scan-helper.php' => ['PhanNoopNew', 'PhanSuspiciousValueComparison', 'PhanTypeConversionFromArray', 'PhanTypeMismatchReturnProbablyReal'], 'modules/class-sync-data-settings-tester.php' => ['PhanNoopNew', 'PhanTypePossiblyInvalidDimOffset', 'PhanUndeclaredClass'], 'modules/class-waf-helper.php' => ['PhanNoopNew', 'PhanPluginSimplifyExpressionBool', 'PhanTypeMismatchReturnProbablyReal', 'PhanUndeclaredClassConstant', 'PhanUndeclaredClassMethod'], + 'modules/class-wpcom-api-request-faker-module.php' => ['PhanUndeclaredClassMethod'], 'modules/class-wpcom-api-request-tracker-module.php' => ['PhanNoopNew', 'PhanTypeMismatchArgument'], 'modules/class-xmlrpc-blocker.php' => ['PhanNoopNew'], 'modules/class-xmlrpc-logger.php' => ['PhanNoopNew', 'PhanUndeclaredFunction'], diff --git a/projects/plugins/debug-helper/changelog/debug-add-wpcom-api-request-faker b/projects/plugins/debug-helper/changelog/debug-add-wpcom-api-request-faker new file mode 100644 index 0000000000000..6b85711b72655 --- /dev/null +++ b/projects/plugins/debug-helper/changelog/debug-add-wpcom-api-request-faker @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Debug Helper: Added WPcom API request sending functionality to help testing specific requests manually. diff --git a/projects/plugins/debug-helper/modules/class-wpcom-api-request-faker-module.php b/projects/plugins/debug-helper/modules/class-wpcom-api-request-faker-module.php new file mode 100644 index 0000000000000..b50bdf0e6d16f --- /dev/null +++ b/projects/plugins/debug-helper/modules/class-wpcom-api-request-faker-module.php @@ -0,0 +1,185 @@ +Error: This helper requires a jetpack connection to work. Please ensure that you have set one up before using.

'; + return; + } + + // Handle the form submit + if ( ! empty( $_POST ) ) { + if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ?? '' ) ), 'wpcom-api-request-faker' ) ) { + echo '

Wrong nonce, aborting.

'; + return; + } + + $is_connected = ( new Connection_Manager() )->is_connected(); + if ( ! $is_connected ) { + echo '

Site is not connected, please establish a jetpack connection first

'; + return; + } + + $api_url = '/' . sanitize_text_field( wp_unslash( $_POST['url'] ?? '' ) ); + $version = sanitize_text_field( wp_unslash( $_POST['version'] ?? '2' ) ); + $method = sanitize_text_field( wp_unslash( $_POST['method'] ?? 'get' ) ); + + $body = null; + if ( 'post' === $_POST['method'] || 'put' === $_POST['method'] ) { + $body = json_decode( sanitize_text_field( wp_unslash( $_POST['body'] ?? '' ) ), true ); + } + + $response = Client::wpcom_json_api_request_as_blog( + $api_url, + $version, + array( 'method' => sanitize_text_field( wp_unslash( $_POST['method'] ?? '2' ) ) ), + $body, + 'wpcom' + ); + + $response_code = wp_remote_retrieve_response_code( $response ); + + // Display error or response + if ( is_wp_error( $response ) || 200 !== $response_code || empty( $response['body'] ) ) { + ?> +

Something went wrong, here is the error (http code )

+ +
+ +

Response for

+ ' . esc_html( var_export( $looks_like_json ? json_decode( $body, true ) : $body, true ) ) . ''; + } + } + + ?> +
+ +

WPcom API Request Faker

+ +
+
+
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ / + +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ + +
+ +
+
+
+
+ 'WPCOM API Request Tracker', 'description' => 'Displays the number of requests to WPCOM API endpoints for the current page request.', ), + 'wpcom-api-request-faker' => array( + 'file' => 'class-wpcom-api-request-faker-module.php', + 'name' => 'WPCOM API Request Faker', + 'description' => 'Send custom requests to the WPcom API, authorized via your Jetpack connection.', + ), 'xmlrpc-logger' => array( 'file' => 'class-xmlrpc-logger.php', 'name' => 'XMLRPC Logger',