class-schema.php
3.65 KB
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
<?php
/**
* The Schema for Rest endpoint
*
* @link https://wordpress.org/plugins/broken-link-checker/
* @since 2.0.0
*
* @author WPMUDEV (https://wpmudev.com)
* @package WPMUDEV_BLC\App\Rest_Endpoints\Settings
*/
namespace WPMUDEV_BLC\App\Rest_Endpoints\Settings\Includes;
// Abort if called directly.
defined( 'WPINC' ) || die;
class Schema {
/**
* Get Schema for Rest Endpoint.
*
* @since 1.0.0
*
* @param string $action A string that contains action that endpoint performs.
*
* @param array $args An array containing several options that we can use for returning specific schema properties.
*
* @return array An array containing Schema.
*/
public static function get_schema( string $action = null, array $args = array() ) {
if ( \is_null( $action ) ) {
return array();
}
$poperties_keys = array();
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => isset( $args['rest_base'] ) ? $args['rest_base'] : '',
'type' => 'object',
'properties' => array(),
);
switch ( $action ) {
case 'save':
$poperties_keys = array( 'message', 'status_code' );
break;
case 'get':
$poperties_keys = array( 'message', 'status_code', 'settings' );
break;
}
$schema['properties'] = self::get_schema_properties( $poperties_keys );
return $schema;
}
/**
* Get Schema properties for Rest Response.
*
* @since 1.0.0
*
* @param array $properties_keys An array containing field keys for properties needed.
*
* @return array An array of schema properties.
*/
protected static function get_schema_properties( array $properties_keys = array() ) {
$return_properties = array();
$schema_properties = array(
'settings' => array(
'description' => esc_html__( 'All BLC settings.', 'broken-link-checker' ),
'type' => 'object',
'properties' => array(
'activation_modal_shown' => array(
'description' => esc_html__( 'Activation modal shown or not.', 'broken-link-checker' ),
'type' => 'string',
),
'use_legacy_blc_version' => array(
'description' => esc_html__( 'Use legacy BLC', 'broken-link-checker' ),
'type' => 'string',
),
'userRolesAllowed' => array(
'type' => 'object',
'properties' => array(
'name' => array(
'description' => esc_html__( 'User role name', 'broken-link-checker' ),
'type' => 'string',
),
'label' => array(
'description' => esc_html__( 'User role label', 'broken-link-checker' ),
'type' => 'string',
),
),
),
),
),
'message' => array(
'description' => esc_html__( 'Response message.', 'broken-link-checker' ),
'type' => 'string',
),
'status_code' => array(
'description' => esc_html__( 'Response status code.', 'broken-link-checker' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
'enum' => array(
'200',
'400',
'401',
'403',
),
'readonly' => true,
),
'instructions' => array(
'description' => esc_html__( 'Response instructions.', 'broken-link-checker' ),
'type' => 'object',
),
);
if ( empty( $properties_keys ) ) {
$return_properties = $schema_properties;
} else {
$return_properties = \array_filter(
$schema_properties,
function( string $property_key = '' ) use ( $properties_keys ) {
return in_array( $property_key, $properties_keys );
},
ARRAY_FILTER_USE_KEY
);
}
return apply_filters( 'wpmudev_blc_rest_enpoints_settings_schema_properties', $return_properties );
}
}