-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathswaggersetting.php
53 lines (37 loc) · 1.44 KB
/
swaggersetting.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
<?php
class SwaggerSetting {
public function menu() {
add_submenu_page( 'options-general.php', 'Swagger Setting', 'Swagger', 'manage_options', 'swagger-ui', [ $this, 'display' ] );
}
public function saveSetting() {
if ( isset( $_POST['_wpnonce'] ) && current_user_can( 'manage_options' ) && wp_verify_nonce( $_POST['_wpnonce'], 'swagger_api_setting' ) ) {
if ( isset( $_POST['swagger_api_basepath'] ) ) {
update_option( 'swagger_api_basepath', sanitize_text_field( $_POST['swagger_api_basepath'] ) );
}
add_action( 'admin_notices', [ $this, 'notices' ] );
}
}
public function notices() {
echo self::template( 'notice' );
}
public function display() {
$data = [];
$data['page_title'] = get_admin_page_title();
$data['swagger_api_basepath'] = WP_API_SwaggerUI::getCLeanNameSpace();
$data['namespaces'] = rest_get_server()->get_namespaces();
$data['docs_url'] = home_url( untrailingslashit( WP_API_SwaggerUI::rewriteBaseApi() ) . '/docs' );
echo self::template( 'setting', $data );
}
public static function template( $file, $data = [] ) {
ob_start();
$__file = __DIR__ . DIRECTORY_SEPARATOR . 'template/' . $file . '.php';
if ( is_readable( $__file ) ) {
extract( $data, EXTR_SKIP );
include $__file;
}
return ob_get_clean();
}
}
$swaggerSetting = new SwaggerSetting();
add_action( 'admin_menu', [ $swaggerSetting, 'menu' ] );
add_action( 'init', [ $swaggerSetting, 'saveSetting' ] );