-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwp-openapi.php
267 lines (229 loc) · 12.3 KB
/
wp-openapi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
/**
* WP OpenAPI
*
* @package WP OpenAPI
* @author Moon
* @copyright Moon
* @license GPL-2.0-or-later
*
* Plugin Name: WP OpenAPI
* Plugin URI: https://github.com/moon0326/wp-openapi
* Version: 1.0.18
* Author: Moon K
* Author URI: https://github.com/moon0326
* License: GPL v2 or later
* Requires PHP: 7.1
* Description: WP OpenAPI outputs the OpenAPI 3.1.0 spec and provides a viewer for your WordPress REST APIs.
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
use WPOpenAPI\CLI\ExportAsHTML;
use WPOpenAPI\CLI\ExportAsJSON;
use WPOpenAPI\Filters;
use WPOpenAPI\Filters\AddCallbackInfoToDescription;
use WPOpenAPI\SchemaGenerator;
use WPOpenAPI\SettingsPage;
use WPOpenAPI\View;
require_once __DIR__ . '/vendor/autoload.php';
if ( ! defined( 'WP_OPENAPI_PLUGIN_PATH' ) ) {
define( 'WP_OPENAPI_PLUGIN_PATH', __DIR__ );
}
new SettingsPage();
class WPOpenAPI {
public static function pluginPath( $append = '' ) {
return plugin_dir_path( __FILE__ ) . $append;
}
public static function pluginUrl( $path = null ) {
return plugin_dir_url( __FILE__ ) . $path;
}
public function registerRoutes() {
add_rewrite_tag( '%openapi%', '([^&]+)' );
add_rewrite_rule( '^' . 'wp-json-openapi/?', 'index.php?openapi=schema', 'top' );
}
public function registerRestAPIEndpoint() {
register_rest_route(
'wp-openapi/v1',
'schema',
array(
'methods' => 'GET',
'callback' => array( $this, 'sendOpenAPISchema' ),
'permission_callback' => function() {
return apply_filters( Filters::PREFIX . 'filter-schema-endpoint-permission', true );
},
'args' => array(
'namespace' => array(
'type' => 'string',
),
),
)
);
}
private function getNamespace() {
$namespace = 'all';
$requestedNamespace = isset( $_GET['namespace'] ) ? str_replace( 'http://', '', sanitize_url( $_GET['namespace'], 'http' ) ) : null;
$requestedPage = isset( $_GET['page'] ) ? str_replace( 'http://', '', sanitize_url( $_GET['page'], 'http' ) ) : null;
if ( $requestedNamespace ) {
$namespace = $requestedNamespace;
} elseif ( $requestedPage && str_contains( $requestedPage, 'wp-openapi/' ) ) {
$namespace = str_replace( 'wp-openapi/', '', $requestedPage );
}
return $namespace;
}
public function addAdminMenu() {
$logo = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI2NCIgaGVpZ2h0PSI2NCI+PGcgdHJhbnNmb3JtPSJtYXRyaXgoLjU3MTM2MSAwIDAgLjU3MTM2MSAtMS43MTQwNDMgLTEuNzEzOTM1KSI+PHBhdGggZD0iTTMyLjYgNjcuNEgzLjA0NGwuMDE3LjQzMy4wNS44MzMuMDIyLjM1Ni4wOC45NjIuMDIuMjE1LjExNSAxLjA2NWExLjE2IDEuMTYgMCAwIDAgLjAxMS4xMDNsLjE1IDEuM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slug = 'wp-openapi';
$callback = function () {
$this->enqueueScritps();
echo "<div id='elements-app'></div>";
};
add_menu_page( 'WP OpenAPI', 'WP OpenAPI', 'read', $slug, $callback, $logo );
add_submenu_page( $slug, 'All', 'All', 'read', $slug, $callback );
foreach ( rest_get_server()->get_namespaces() as $namespace ) {
if (strpos($namespace, 'wp-openapi') === 0) {
continue;
}
add_submenu_page(
$slug,
$namespace,
$namespace,
'read',
$slug . '/' . $namespace,
$callback
);
}
}
public function getAssetInfo( $name = '' ) {
global $wp_version;
$info = array(
'dependencies' => array(),
'version' => $wp_version,
);
$file = self::pluginPath( $name . '.asset.php' );
if ( is_readable( $file ) ) {
$info = include $file;
}
return $info;
}
public function sendOpenAPISchema() {
global $wp_version;
$siteInfo = array(
'admin_email' => get_option( 'admin_email' ),
'blogname' => get_option( 'blogname' ),
'blogdescription' => get_option( 'blogdescription' ),
'home' => get_option( 'home' ),
'wp_version' => $wp_version,
);
$restServer = rest_get_server();
$hooks = Filters::getInstance();
if ( SettingsPage::getOption( 'enableCallbackDiscovery' ) === 'on' ) {
new AddCallbackInfoToDescription( $hooks, new View( 'callback' ), $restServer->get_routes() );
}
$schemaGenerator = new SchemaGenerator( $hooks, $siteInfo, $restServer );
wp_send_json( $schemaGenerator->generate( $this->getNamespace() ) );
}
public function enqueueScritps() {
$infoCss = $this->getAssetInfo( 'build/resources/css/app' );
$infoJs = $this->getAssetInfo( 'build/resources/scripts/app' );
wp_enqueue_style(
'wp-openapi-scss',
self::pluginUrl( 'build/resources/css/app.css' ),
$infoCss['dependencies'],
$infoCss['version']
);
wp_enqueue_style(
'wp-openapi-js-css',
self::pluginUrl( 'build/resources/scripts/app.css' ),
array(),
$infoJs['version']
);
wp_enqueue_script(
'wp-openapi-js',
self::pluginUrl( 'build/resources/scripts/app.js' ),
$infoJs['dependencies'],
$infoJs['version']
);
$endpoint = add_query_arg( 'namespace', $this->getNamespace(), rest_url( 'wp-openapi/v1/schema' ) );
$data = array(
'options' => array(
'hideTryIt' => SettingsPage::getOption( 'enableTryIt' ) === null,
),
'endpoint' => $endpoint,
);
if ( is_admin() && current_user_can( 'edit_posts' ) ) {
$data['nonce'] = wp_create_nonce( 'wp_rest' );
} else {
$data['options']['hideTryIt'] = true;
}
$data = apply_filters( 'wp-openapi-filters-elements-props', $data );
wp_localize_script( 'wp-openapi-js', 'wpOpenApi', $data );
}
}
$wpOpenAPI = new WPOpenAPI();
register_activation_hook(
__FILE__,
function () use ( $wpOpenAPI ) {
update_option( 'wp-openapi-rewrite-flushed', false );
}
);
register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
add_action( 'admin_menu', array( $wpOpenAPI, 'addAdminMenu' ) );
add_action(
'init',
function() use ( $wpOpenAPI ) {
$wpOpenAPI->registerRoutes();
if ( !get_option( 'wp-openapi-rewrite-flushed' ) ) {
flush_rewrite_rules();
update_option( 'wp-openapi-rewrite-flushed', true );
}
}
);
add_action( 'rest_api_init', array( $wpOpenAPI, 'registerRestAPIEndpoint' ) );
add_action(
'wp',
function () use ( $wpOpenAPI ) {
if ( get_query_var( 'openapi' ) === 'schema' && apply_filters( Filters::PREFIX . 'filter-schema-endpoint-permission', true ) === true ) {
$wpOpenAPI->sendOpenAPISchema();
}
}
);
add_action(
'cli_init',
function () {
WP_CLI::add_command(
'openapi export',
function ( $args, $assoc_args ) {
$namespace = $assoc_args['namespace'];
$saveTo = $assoc_args['save_to'];
$format = $assoc_args['format'];
if ( $format === 'json' ) {
$command = new ExportAsJSON();
$command->execute( $namespace, $saveTo );
return;
}
$command = new ExportAsHTML();
$command->execute( $namespace, $saveTo );
},
array(
'synopsis' => array(
array(
'name' => 'namespace',
'type' => 'assoc',
'optional' => false,
),
array(
'name' => 'save_to',
'type' => 'assoc',
'optional' => false,
),
array(
'name' => 'format',
'type' => 'assoc',
'optional' => true,
'default' => 'html',
'options' => array( 'html', 'json' ),
)
),
)
);
}
);