|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* REST API Tester file contains the class `REST_API_Tester` that tests REST API endpoints. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Jetpack |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Debug_Helper; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* REST_API_Tester to test REST API endpoints. |
|
12
|
|
|
*/ |
|
13
|
|
|
class REST_API_Tester { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Construction. |
|
17
|
|
|
*/ |
|
18
|
|
|
public function __construct() { |
|
19
|
|
|
add_action( 'admin_menu', array( $this, 'register_submenu_page' ), 1000 ); |
|
20
|
|
|
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Add submenu item. |
|
25
|
|
|
*/ |
|
26
|
|
|
public function register_submenu_page() { |
|
27
|
|
|
add_submenu_page( |
|
28
|
|
|
'jetpack', |
|
29
|
|
|
'REST API Tester', |
|
30
|
|
|
'REST API Tester', |
|
31
|
|
|
'manage_options', |
|
32
|
|
|
'rest-api-tester', |
|
33
|
|
|
array( $this, 'render_ui' ), |
|
34
|
|
|
99 |
|
35
|
|
|
); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Enqueue scripts! |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $hook Page hook. |
|
42
|
|
|
*/ |
|
43
|
|
|
public function enqueue_scripts( $hook ) { |
|
44
|
|
|
if ( strpos( $hook, 'jetpack_page_rest-api-tester' ) === 0 ) { |
|
45
|
|
|
wp_enqueue_style( 'rest_api_tester_style', plugin_dir_url( __FILE__ ) . 'inc/css/rest-api-tester.css', array(), JETPACK_DEBUG_HELPER_VERSION ); |
|
46
|
|
|
wp_enqueue_script( 'rest_api_tester_script', plugin_dir_url( __FILE__ ) . 'inc/js/rest-api-tester.js', array( 'wp-api' ), JETPACK_DEBUG_HELPER_VERSION, true ); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Render UI. |
|
52
|
|
|
*/ |
|
53
|
|
|
public function render_ui() { |
|
54
|
|
|
wp_localize_script( |
|
55
|
|
|
'wp-api', |
|
56
|
|
|
'wpApiSettings', |
|
57
|
|
|
array( |
|
58
|
|
|
'root' => esc_url_raw( rest_url() ), |
|
59
|
|
|
'nonce' => wp_create_nonce( 'wp_rest' ), |
|
60
|
|
|
) |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
|
|
?> |
|
64
|
|
|
<h1>REST API Tester</h1> |
|
65
|
|
|
|
|
66
|
|
|
<div class="jetpack-debug-api-tester"> |
|
67
|
|
|
<form method="post" id="jetpack-debug-api-tester-form"> |
|
68
|
|
|
<div class="api-tester-block"> |
|
69
|
|
|
<label for="api-tester-method">Method:</label> |
|
70
|
|
|
<div class="api-tester-field"> |
|
71
|
|
|
<select name="method" id="api-tester-method"> |
|
72
|
|
|
<option value="get">GET</option> |
|
73
|
|
|
<option value="post">POST</option> |
|
74
|
|
|
<option value="put">PUT</option> |
|
75
|
|
|
<option value="delete">DELETE</option> |
|
76
|
|
|
</select> |
|
77
|
|
|
</div> |
|
78
|
|
|
</div> |
|
79
|
|
|
|
|
80
|
|
|
<div class="api-tester-block"> |
|
81
|
|
|
<label for="api-tester-url">REST Route:</label> |
|
82
|
|
|
<div class="api-tester-field"> |
|
83
|
|
|
<span class="rest-route-prefix">/jetpack/v4/</span> |
|
84
|
|
|
<input type="text" name="url" class="input-url" id="api-tester-url"> |
|
85
|
|
|
</div> |
|
86
|
|
|
</div> |
|
87
|
|
|
|
|
88
|
|
|
<div class="api-tester-block api-tester-filter-post block-hide"> |
|
89
|
|
|
<label for="api-tester-content-type">Content-Type:</label> |
|
90
|
|
|
<div class="api-tester-field"> |
|
91
|
|
|
<select name="content-type" id="api-tester-content-type"> |
|
92
|
|
|
<option name="application/json">application/json</option> |
|
93
|
|
|
</select> |
|
94
|
|
|
</div> |
|
95
|
|
|
</div> |
|
96
|
|
|
|
|
97
|
|
|
<div class="api-tester-block api-tester-filter-post block-hide"> |
|
98
|
|
|
<label for="api-tester-body">Body:</label> |
|
99
|
|
|
<div class="api-tester-field"> |
|
100
|
|
|
<textarea name="body" id="api-tester-body"></textarea> |
|
101
|
|
|
</div> |
|
102
|
|
|
</div> |
|
103
|
|
|
|
|
104
|
|
|
<div class="api-tester-block align-right"> |
|
105
|
|
|
<button type="submit" class="button-right" id="api-tester-submit">Send</button> |
|
106
|
|
|
</div> |
|
107
|
|
|
|
|
108
|
|
|
<div id="api-tester-response" class="block-hide"></div> |
|
109
|
|
|
</form> |
|
110
|
|
|
</div> |
|
111
|
|
|
<?php |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Load the class. |
|
116
|
|
|
*/ |
|
117
|
|
|
public static function register_rest_api_tester() { |
|
118
|
|
|
if ( class_exists( 'Jetpack' ) ) { |
|
119
|
|
|
new REST_API_Tester(); |
|
120
|
|
|
} else { |
|
121
|
|
|
add_action( 'admin_notices', array( __CLASS__, 'jetpack_not_active' ) ); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Jetpack is missing ¯\_(ツ)_/¯. |
|
127
|
|
|
*/ |
|
128
|
|
|
public function jetpack_not_active() { |
|
129
|
|
|
echo '<div class="notice info"><p>Jetpack needs to be active and installed for the Broken Token plugin.</p></div>'; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
add_action( 'plugins_loaded', array( REST_API_Tester::class, 'register_rest_api_tester' ), 1000 ); |
|
135
|
|
|
|