Completed
Push — update/react-dash-wordpress-i1... ( 3ef406...9a5312 )
by Jeremy
45:47 queued 38:34
created

REST_API_Tester::jetpack_not_active()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
		}
121
	}
122
}
123
124
add_action( 'plugins_loaded', array( REST_API_Tester::class, 'register_rest_api_tester' ), 1000 );
125