Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a debug tool to send API requests to WPcom via the Jetpack connection. #41154

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Debug Helper: Added WPcom API request sending functionality to help testing specific requests manually.
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php
/**
* Plugin Name: WPCOM API Request Faker
* Description: Send custom requests to the WPcom API, authorized via your Jetpack connection.
* Author: Bestpack
* Version: 1.0
* Text Domain: jetpack
*
* @package automattic/jetpack-debug-helper
*/

use Automattic\Jetpack\Connection\Client;
use Automattic\Jetpack\Connection\Manager as Connection_Manager;

/**
* Class WPCOM_API_Request_Faker_Module
*/
class WPCOM_API_Request_Faker_Module {

/**
* WPCOM_API_Request_Faker constructor.
*/
public function __construct() {
add_action( 'admin_menu', array( $this, 'register_submenu_page' ), 2000 );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
}

/**
* Add submenu item.
*/
public function register_submenu_page() {
add_submenu_page(
'jetpack-debug-tools',
'WPcom API Request Faker',
'WPcom API Request Faker',
'manage_options',
'wpcom-api-request-faker',
array( $this, 'render' ),
9999
);
}

/**
* Enqueue scripts!
*
* @param string $hook Page hook.
*/
public function enqueue_scripts( $hook ) {
if ( str_starts_with( $hook, 'jetpack-debug_page_wpcom-api-request-faker' ) ) {
wp_enqueue_style( 'rest_api_tester_style', plugin_dir_url( __FILE__ ) . 'inc/css/rest-api-tester.css', array(), JETPACK_DEBUG_HELPER_VERSION );
}
}

/**
* Render the details panel.
*/
public function render() {
$version = '2';

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

$is_connected = ( new Connection_Manager() )->is_connected();

Check failure on line 67 in projects/plugins/debug-helper/modules/class-wpcom-api-request-faker-module.php

View workflow job for this annotation

GitHub Actions / Static analysis

UndefError PhanUndeclaredClassMethod Call to method __construct from undeclared class \Automattic\Jetpack\Connection\Manager Suggestion: Did you mean class \MongoDB\Driver\Manager

Check failure on line 67 in projects/plugins/debug-helper/modules/class-wpcom-api-request-faker-module.php

View workflow job for this annotation

GitHub Actions / Static analysis

UndefError PhanUndeclaredClassMethod Call to method is_connected from undeclared class \Automattic\Jetpack\Connection\Manager Suggestion: Did you mean class \MongoDB\Driver\Manager
if ( ! $is_connected ) {
echo '<p>Site is not connected, please establish a jetpack connection first</p>';
return;
}

$api_url = '/' . sanitize_text_field( wp_unslash( $_POST['url'] ?? '' ) );
$version = sanitize_text_field( wp_unslash( $_POST['version'] ?? '2' ) );

$response = Client::wpcom_json_api_request_as_blog(

Check failure on line 76 in projects/plugins/debug-helper/modules/class-wpcom-api-request-faker-module.php

View workflow job for this annotation

GitHub Actions / Static analysis

UndefError PhanUndeclaredClassMethod Call to method wpcom_json_api_request_as_blog from undeclared class \Automattic\Jetpack\Connection\Client
$api_url,
$version,
array( 'method' => 'GET' ),
null,
'wpcom'
);

$response_code = wp_remote_retrieve_response_code( $response );

// Display error or response
if ( is_wp_error( $response ) || 200 !== $response_code || empty( $response['body'] ) ) {
?>
<h2>Something went wrong, here is the error (http code <?php echo esc_html( $response_code ); ?>)</h2>
<?php // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export ?>
<pre><?php echo esc_html( var_export( $response, true ) ); ?></pre>
<?php
} else {
$human_api_url = '/wpcom/v' . $version . $api_url;
?>
<h2>Response for <?php echo esc_html( $human_api_url ); ?></h2>
<?php
$body = wp_remote_retrieve_body( $response );
$looks_like_json = json_decode( $body ) !== null;
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export
echo '<pre>' . esc_html( var_export( $looks_like_json ? json_decode( $body, true ) : $body, true ) ) . '</pre>';
}
}

?>
<div id='wpcom-api-request-faker'>

<h1>WPcom API Request Faker</h1>

<div class="jetpack-debug-api-tester">
<form method="post" id="jetpack-debug-api-tester-form">
<div class="api-tester-block">
<label for="api-tester-method">Method:</label>
<div class="api-tester-field">
<select name="method" id="api-tester-method">
<option value="get">GET</option>
</select>
</div>
</div>

<div class="api-tester-block">
<label for="api-tester-version">Version:</label>
<div class="api-tester-field">
<select name="version" id="api-tester-version">
<option <?php echo $version === '2' ? 'selected="selected"' : ''; ?>>2</option>
<option <?php echo $version === '3' ? 'selected="selected"' : ''; ?>>3</option>
<option <?php echo $version === '4' ? 'selected="selected"' : ''; ?>>4</option>
</select>
</div>
</div>

<div class="api-tester-block">
<label for="api-tester-url">REST Route:</label>
<div class="api-tester-field">
<span class="rest-route-prefix">/</span>
<input type="text" name="url" class="input-url" id="api-tester-url" value="<?php echo esc_attr( sanitize_text_field( wp_unslash( $_POST['url'] ?? '' ) ) ); ?>">
</div>
</div>

<div class="api-tester-block align-right">
<input type="hidden" name="nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpcom-api-request-faker' ) ); ?>">
<button type="submit" class="button-right" id="api-tester-submit">Send</button>
</div>

<div id="api-tester-response" class="block-hide"></div>
</form>
</div>
</div>
<?php
}
}

new WPCOM_API_Request_Faker_Module();

Check failure on line 153 in projects/plugins/debug-helper/modules/class-wpcom-api-request-faker-module.php

View workflow job for this annotation

GitHub Actions / Static analysis

NOOPError PhanNoopNew Unused result of new object creation expression in new WPCOM_API_Request_Faker_Module() (this may be called for the side effects of the non-empty constructor or destructor)
5 changes: 5 additions & 0 deletions projects/plugins/debug-helper/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@
'name' => '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',
Expand Down
Loading