43 * |
43 * |
44 * @see register_rest_route() |
44 * @see register_rest_route() |
45 */ |
45 */ |
46 public function register_routes() { |
46 public function register_routes() { |
47 |
47 |
48 register_rest_route( $this->namespace, '/' . $this->rest_base, array( |
48 register_rest_route( |
|
49 $this->namespace, |
|
50 '/' . $this->rest_base, |
49 array( |
51 array( |
50 'methods' => WP_REST_Server::READABLE, |
52 array( |
51 'callback' => array( $this, 'get_items' ), |
53 'methods' => WP_REST_Server::READABLE, |
52 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
54 'callback' => array( $this, 'get_items' ), |
53 'args' => $this->get_collection_params(), |
55 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
54 ), |
56 'args' => $this->get_collection_params(), |
|
57 ), |
|
58 array( |
|
59 'methods' => WP_REST_Server::CREATABLE, |
|
60 'callback' => array( $this, 'create_item' ), |
|
61 'permission_callback' => array( $this, 'create_item_permissions_check' ), |
|
62 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), |
|
63 ), |
|
64 'schema' => array( $this, 'get_public_item_schema' ), |
|
65 ) |
|
66 ); |
|
67 |
|
68 register_rest_route( |
|
69 $this->namespace, |
|
70 '/' . $this->rest_base . '/(?P<id>[\d]+)', |
55 array( |
71 array( |
56 'methods' => WP_REST_Server::CREATABLE, |
72 'args' => array( |
57 'callback' => array( $this, 'create_item' ), |
73 'id' => array( |
58 'permission_callback' => array( $this, 'create_item_permissions_check' ), |
74 'description' => __( 'Unique identifier for the user.' ), |
59 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), |
75 'type' => 'integer', |
60 ), |
76 ), |
61 'schema' => array( $this, 'get_public_item_schema' ), |
77 ), |
62 ) ); |
78 array( |
63 |
79 'methods' => WP_REST_Server::READABLE, |
64 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( |
80 'callback' => array( $this, 'get_item' ), |
65 'args' => array( |
81 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
66 'id' => array( |
82 'args' => array( |
67 'description' => __( 'Unique identifier for the user.' ), |
83 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
68 'type' => 'integer', |
84 ), |
69 ), |
85 ), |
70 ), |
86 array( |
|
87 'methods' => WP_REST_Server::EDITABLE, |
|
88 'callback' => array( $this, 'update_item' ), |
|
89 'permission_callback' => array( $this, 'update_item_permissions_check' ), |
|
90 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
|
91 ), |
|
92 array( |
|
93 'methods' => WP_REST_Server::DELETABLE, |
|
94 'callback' => array( $this, 'delete_item' ), |
|
95 'permission_callback' => array( $this, 'delete_item_permissions_check' ), |
|
96 'args' => array( |
|
97 'force' => array( |
|
98 'type' => 'boolean', |
|
99 'default' => false, |
|
100 'description' => __( 'Required to be true, as users do not support trashing.' ), |
|
101 ), |
|
102 'reassign' => array( |
|
103 'type' => 'integer', |
|
104 'description' => __( 'Reassign the deleted user\'s posts and links to this user ID.' ), |
|
105 'required' => true, |
|
106 'sanitize_callback' => array( $this, 'check_reassign' ), |
|
107 ), |
|
108 ), |
|
109 ), |
|
110 'schema' => array( $this, 'get_public_item_schema' ), |
|
111 ) |
|
112 ); |
|
113 |
|
114 register_rest_route( |
|
115 $this->namespace, |
|
116 '/' . $this->rest_base . '/me', |
71 array( |
117 array( |
72 'methods' => WP_REST_Server::READABLE, |
118 array( |
73 'callback' => array( $this, 'get_item' ), |
119 'methods' => WP_REST_Server::READABLE, |
74 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
120 'callback' => array( $this, 'get_current_item' ), |
75 'args' => array( |
121 'args' => array( |
76 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
122 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
77 ), |
|
78 ), |
|
79 array( |
|
80 'methods' => WP_REST_Server::EDITABLE, |
|
81 'callback' => array( $this, 'update_item' ), |
|
82 'permission_callback' => array( $this, 'update_item_permissions_check' ), |
|
83 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
|
84 ), |
|
85 array( |
|
86 'methods' => WP_REST_Server::DELETABLE, |
|
87 'callback' => array( $this, 'delete_item' ), |
|
88 'permission_callback' => array( $this, 'delete_item_permissions_check' ), |
|
89 'args' => array( |
|
90 'force' => array( |
|
91 'type' => 'boolean', |
|
92 'default' => false, |
|
93 'description' => __( 'Required to be true, as users do not support trashing.' ), |
|
94 ), |
123 ), |
95 'reassign' => array( |
124 ), |
96 'type' => 'integer', |
125 array( |
97 'description' => __( 'Reassign the deleted user\'s posts and links to this user ID.' ), |
126 'methods' => WP_REST_Server::EDITABLE, |
98 'required' => true, |
127 'callback' => array( $this, 'update_current_item' ), |
99 'sanitize_callback' => array( $this, 'check_reassign' ), |
128 'permission_callback' => array( $this, 'update_current_item_permissions_check' ), |
|
129 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
|
130 ), |
|
131 array( |
|
132 'methods' => WP_REST_Server::DELETABLE, |
|
133 'callback' => array( $this, 'delete_current_item' ), |
|
134 'permission_callback' => array( $this, 'delete_current_item_permissions_check' ), |
|
135 'args' => array( |
|
136 'force' => array( |
|
137 'type' => 'boolean', |
|
138 'default' => false, |
|
139 'description' => __( 'Required to be true, as users do not support trashing.' ), |
|
140 ), |
|
141 'reassign' => array( |
|
142 'type' => 'integer', |
|
143 'description' => __( 'Reassign the deleted user\'s posts and links to this user ID.' ), |
|
144 'required' => true, |
|
145 'sanitize_callback' => array( $this, 'check_reassign' ), |
|
146 ), |
100 ), |
147 ), |
101 ), |
148 ), |
102 ), |
149 'schema' => array( $this, 'get_public_item_schema' ), |
103 'schema' => array( $this, 'get_public_item_schema' ), |
150 ) |
104 ) ); |
151 ); |
105 |
|
106 register_rest_route( $this->namespace, '/' . $this->rest_base . '/me', array( |
|
107 array( |
|
108 'methods' => WP_REST_Server::READABLE, |
|
109 'callback' => array( $this, 'get_current_item' ), |
|
110 'args' => array( |
|
111 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
|
112 ), |
|
113 ), |
|
114 array( |
|
115 'methods' => WP_REST_Server::EDITABLE, |
|
116 'callback' => array( $this, 'update_current_item' ), |
|
117 'permission_callback' => array( $this, 'update_current_item_permissions_check' ), |
|
118 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
|
119 ), |
|
120 array( |
|
121 'methods' => WP_REST_Server::DELETABLE, |
|
122 'callback' => array( $this, 'delete_current_item' ), |
|
123 'permission_callback' => array( $this, 'delete_current_item_permissions_check' ), |
|
124 'args' => array( |
|
125 'force' => array( |
|
126 'type' => 'boolean', |
|
127 'default' => false, |
|
128 'description' => __( 'Required to be true, as users do not support trashing.' ), |
|
129 ), |
|
130 'reassign' => array( |
|
131 'type' => 'integer', |
|
132 'description' => __( 'Reassign the deleted user\'s posts and links to this user ID.' ), |
|
133 'required' => true, |
|
134 'sanitize_callback' => array( $this, 'check_reassign' ), |
|
135 ), |
|
136 ), |
|
137 ), |
|
138 'schema' => array( $this, 'get_public_item_schema' ), |
|
139 )); |
|
140 } |
152 } |
141 |
153 |
142 /** |
154 /** |
143 * Checks for a valid value for the reassign parameter when deleting users. |
155 * Checks for a valid value for the reassign parameter when deleting users. |
144 * |
156 * |
1148 $schema = array( |
1180 $schema = array( |
1149 '$schema' => 'http://json-schema.org/draft-04/schema#', |
1181 '$schema' => 'http://json-schema.org/draft-04/schema#', |
1150 'title' => 'user', |
1182 'title' => 'user', |
1151 'type' => 'object', |
1183 'type' => 'object', |
1152 'properties' => array( |
1184 'properties' => array( |
1153 'id' => array( |
1185 'id' => array( |
1154 'description' => __( 'Unique identifier for the user.' ), |
1186 'description' => __( 'Unique identifier for the user.' ), |
1155 'type' => 'integer', |
1187 'type' => 'integer', |
1156 'context' => array( 'embed', 'view', 'edit' ), |
1188 'context' => array( 'embed', 'view', 'edit' ), |
1157 'readonly' => true, |
1189 'readonly' => true, |
1158 ), |
1190 ), |
1159 'username' => array( |
1191 'username' => array( |
1160 'description' => __( 'Login name for the user.' ), |
1192 'description' => __( 'Login name for the user.' ), |
1161 'type' => 'string', |
1193 'type' => 'string', |
1162 'context' => array( 'edit' ), |
1194 'context' => array( 'edit' ), |
1163 'required' => true, |
1195 'required' => true, |
1164 'arg_options' => array( |
1196 'arg_options' => array( |
1165 'sanitize_callback' => array( $this, 'check_username' ), |
1197 'sanitize_callback' => array( $this, 'check_username' ), |
1166 ), |
1198 ), |
1167 ), |
1199 ), |
1168 'name' => array( |
1200 'name' => array( |
1169 'description' => __( 'Display name for the user.' ), |
1201 'description' => __( 'Display name for the user.' ), |
1170 'type' => 'string', |
1202 'type' => 'string', |
1171 'context' => array( 'embed', 'view', 'edit' ), |
1203 'context' => array( 'embed', 'view', 'edit' ), |
1172 'arg_options' => array( |
1204 'arg_options' => array( |
1173 'sanitize_callback' => 'sanitize_text_field', |
1205 'sanitize_callback' => 'sanitize_text_field', |
1174 ), |
1206 ), |
1175 ), |
1207 ), |
1176 'first_name' => array( |
1208 'first_name' => array( |
1177 'description' => __( 'First name for the user.' ), |
1209 'description' => __( 'First name for the user.' ), |
1178 'type' => 'string', |
1210 'type' => 'string', |
1179 'context' => array( 'edit' ), |
1211 'context' => array( 'edit' ), |
1180 'arg_options' => array( |
1212 'arg_options' => array( |
1181 'sanitize_callback' => 'sanitize_text_field', |
1213 'sanitize_callback' => 'sanitize_text_field', |
1182 ), |
1214 ), |
1183 ), |
1215 ), |
1184 'last_name' => array( |
1216 'last_name' => array( |
1185 'description' => __( 'Last name for the user.' ), |
1217 'description' => __( 'Last name for the user.' ), |
1186 'type' => 'string', |
1218 'type' => 'string', |
1187 'context' => array( 'edit' ), |
1219 'context' => array( 'edit' ), |
1188 'arg_options' => array( |
1220 'arg_options' => array( |
1189 'sanitize_callback' => 'sanitize_text_field', |
1221 'sanitize_callback' => 'sanitize_text_field', |
1190 ), |
1222 ), |
1191 ), |
1223 ), |
1192 'email' => array( |
1224 'email' => array( |
1193 'description' => __( 'The email address for the user.' ), |
1225 'description' => __( 'The email address for the user.' ), |
1194 'type' => 'string', |
1226 'type' => 'string', |
1195 'format' => 'email', |
1227 'format' => 'email', |
1196 'context' => array( 'edit' ), |
1228 'context' => array( 'edit' ), |
1197 'required' => true, |
1229 'required' => true, |
1198 ), |
1230 ), |
1199 'url' => array( |
1231 'url' => array( |
1200 'description' => __( 'URL of the user.' ), |
1232 'description' => __( 'URL of the user.' ), |
1201 'type' => 'string', |
1233 'type' => 'string', |
1202 'format' => 'uri', |
1234 'format' => 'uri', |
1203 'context' => array( 'embed', 'view', 'edit' ), |
1235 'context' => array( 'embed', 'view', 'edit' ), |
1204 ), |
1236 ), |
1205 'description' => array( |
1237 'description' => array( |
1206 'description' => __( 'Description of the user.' ), |
1238 'description' => __( 'Description of the user.' ), |
1207 'type' => 'string', |
1239 'type' => 'string', |
1208 'context' => array( 'embed', 'view', 'edit' ), |
1240 'context' => array( 'embed', 'view', 'edit' ), |
1209 ), |
1241 ), |
1210 'link' => array( |
1242 'link' => array( |
1211 'description' => __( 'Author URL of the user.' ), |
1243 'description' => __( 'Author URL of the user.' ), |
1212 'type' => 'string', |
1244 'type' => 'string', |
1213 'format' => 'uri', |
1245 'format' => 'uri', |
1214 'context' => array( 'embed', 'view', 'edit' ), |
1246 'context' => array( 'embed', 'view', 'edit' ), |
1215 'readonly' => true, |
1247 'readonly' => true, |
1216 ), |
1248 ), |
1217 'locale' => array( |
1249 'locale' => array( |
1218 'description' => __( 'Locale for the user.' ), |
1250 'description' => __( 'Locale for the user.' ), |
1219 'type' => 'string', |
1251 'type' => 'string', |
1220 'enum' => array_merge( array( '', 'en_US' ), get_available_languages() ), |
1252 'enum' => array_merge( array( '', 'en_US' ), get_available_languages() ), |
1221 'context' => array( 'edit' ), |
1253 'context' => array( 'edit' ), |
1222 ), |
1254 ), |
1223 'nickname' => array( |
1255 'nickname' => array( |
1224 'description' => __( 'The nickname for the user.' ), |
1256 'description' => __( 'The nickname for the user.' ), |
1225 'type' => 'string', |
1257 'type' => 'string', |
1226 'context' => array( 'edit' ), |
1258 'context' => array( 'edit' ), |
1227 'arg_options' => array( |
1259 'arg_options' => array( |
1228 'sanitize_callback' => 'sanitize_text_field', |
1260 'sanitize_callback' => 'sanitize_text_field', |
1229 ), |
1261 ), |
1230 ), |
1262 ), |
1231 'slug' => array( |
1263 'slug' => array( |
1232 'description' => __( 'An alphanumeric identifier for the user.' ), |
1264 'description' => __( 'An alphanumeric identifier for the user.' ), |
1233 'type' => 'string', |
1265 'type' => 'string', |
1234 'context' => array( 'embed', 'view', 'edit' ), |
1266 'context' => array( 'embed', 'view', 'edit' ), |
1235 'arg_options' => array( |
1267 'arg_options' => array( |
1236 'sanitize_callback' => array( $this, 'sanitize_slug' ), |
1268 'sanitize_callback' => array( $this, 'sanitize_slug' ), |
1237 ), |
1269 ), |
1238 ), |
1270 ), |
1239 'registered_date' => array( |
1271 'registered_date' => array( |
1240 'description' => __( 'Registration date for the user.' ), |
1272 'description' => __( 'Registration date for the user.' ), |
1241 'type' => 'string', |
1273 'type' => 'string', |
1242 'format' => 'date-time', |
1274 'format' => 'date-time', |
1243 'context' => array( 'edit' ), |
1275 'context' => array( 'edit' ), |
1244 'readonly' => true, |
1276 'readonly' => true, |
1245 ), |
1277 ), |
1246 'roles' => array( |
1278 'roles' => array( |
1247 'description' => __( 'Roles assigned to the user.' ), |
1279 'description' => __( 'Roles assigned to the user.' ), |
1248 'type' => 'array', |
1280 'type' => 'array', |
1249 'items' => array( |
1281 'items' => array( |
1250 'type' => 'string', |
1282 'type' => 'string', |
1251 ), |
1283 ), |
1252 'context' => array( 'edit' ), |
1284 'context' => array( 'edit' ), |
1253 ), |
1285 ), |
1254 'password' => array( |
1286 'password' => array( |
1255 'description' => __( 'Password for the user (never included).' ), |
1287 'description' => __( 'Password for the user (never included).' ), |
1256 'type' => 'string', |
1288 'type' => 'string', |
1257 'context' => array(), // Password is never displayed. |
1289 'context' => array(), // Password is never displayed. |
1258 'required' => true, |
1290 'required' => true, |
1259 'arg_options' => array( |
1291 'arg_options' => array( |
1260 'sanitize_callback' => array( $this, 'check_user_password' ), |
1292 'sanitize_callback' => array( $this, 'check_user_password' ), |
1261 ), |
1293 ), |
1262 ), |
1294 ), |
1263 'capabilities' => array( |
1295 'capabilities' => array( |
1264 'description' => __( 'All capabilities assigned to the user.' ), |
1296 'description' => __( 'All capabilities assigned to the user.' ), |
1265 'type' => 'object', |
1297 'type' => 'object', |
1266 'context' => array( 'edit' ), |
1298 'context' => array( 'edit' ), |
1267 'readonly' => true, |
1299 'readonly' => true, |
1268 ), |
1300 ), |
1315 $query_params = parent::get_collection_params(); |
1347 $query_params = parent::get_collection_params(); |
1316 |
1348 |
1317 $query_params['context']['default'] = 'view'; |
1349 $query_params['context']['default'] = 'view'; |
1318 |
1350 |
1319 $query_params['exclude'] = array( |
1351 $query_params['exclude'] = array( |
1320 'description' => __( 'Ensure result set excludes specific IDs.' ), |
1352 'description' => __( 'Ensure result set excludes specific IDs.' ), |
1321 'type' => 'array', |
1353 'type' => 'array', |
1322 'items' => array( |
1354 'items' => array( |
1323 'type' => 'integer', |
1355 'type' => 'integer', |
1324 ), |
1356 ), |
1325 'default' => array(), |
1357 'default' => array(), |
1326 ); |
1358 ); |
1327 |
1359 |
1328 $query_params['include'] = array( |
1360 $query_params['include'] = array( |
1329 'description' => __( 'Limit result set to specific IDs.' ), |
1361 'description' => __( 'Limit result set to specific IDs.' ), |
1330 'type' => 'array', |
1362 'type' => 'array', |
1331 'items' => array( |
1363 'items' => array( |
1332 'type' => 'integer', |
1364 'type' => 'integer', |
1333 ), |
1365 ), |
1334 'default' => array(), |
1366 'default' => array(), |
1335 ); |
1367 ); |
1336 |
1368 |
1337 $query_params['offset'] = array( |
1369 $query_params['offset'] = array( |
1338 'description' => __( 'Offset the result set by a specific number of items.' ), |
1370 'description' => __( 'Offset the result set by a specific number of items.' ), |
1339 'type' => 'integer', |
1371 'type' => 'integer', |
1340 ); |
1372 ); |
1341 |
1373 |
1342 $query_params['order'] = array( |
1374 $query_params['order'] = array( |
1343 'default' => 'asc', |
1375 'default' => 'asc', |
1344 'description' => __( 'Order sort attribute ascending or descending.' ), |
1376 'description' => __( 'Order sort attribute ascending or descending.' ), |
1345 'enum' => array( 'asc', 'desc' ), |
1377 'enum' => array( 'asc', 'desc' ), |
1346 'type' => 'string', |
1378 'type' => 'string', |
1347 ); |
1379 ); |
1348 |
1380 |
1349 $query_params['orderby'] = array( |
1381 $query_params['orderby'] = array( |
1350 'default' => 'name', |
1382 'default' => 'name', |
1351 'description' => __( 'Sort collection by object attribute.' ), |
1383 'description' => __( 'Sort collection by object attribute.' ), |
1352 'enum' => array( |
1384 'enum' => array( |
1353 'id', |
1385 'id', |
1354 'include', |
1386 'include', |
1355 'name', |
1387 'name', |
1356 'registered_date', |
1388 'registered_date', |
1357 'slug', |
1389 'slug', |
1358 'include_slugs', |
1390 'include_slugs', |
1359 'email', |
1391 'email', |
1360 'url', |
1392 'url', |
1361 ), |
1393 ), |
1362 'type' => 'string', |
1394 'type' => 'string', |
1363 ); |
1395 ); |
1364 |
1396 |
1365 $query_params['slug'] = array( |
1397 $query_params['slug'] = array( |
1366 'description' => __( 'Limit result set to users with one or more specific slugs.' ), |
1398 'description' => __( 'Limit result set to users with one or more specific slugs.' ), |
1367 'type' => 'array', |
1399 'type' => 'array', |
1368 'items' => array( |
1400 'items' => array( |
1369 'type' => 'string', |
1401 'type' => 'string', |
1370 ), |
1402 ), |
1371 ); |
1403 ); |
1372 |
1404 |
1373 $query_params['roles'] = array( |
1405 $query_params['roles'] = array( |
1374 'description' => __( 'Limit result set to users matching at least one specific role provided. Accepts csv list or single role.' ), |
1406 'description' => __( 'Limit result set to users matching at least one specific role provided. Accepts csv list or single role.' ), |
1375 'type' => 'array', |
1407 'type' => 'array', |
1376 'items' => array( |
1408 'items' => array( |
1377 'type' => 'string', |
1409 'type' => 'string', |
1378 ), |
1410 ), |
1379 ); |
1411 ); |
1380 |
1412 |
1381 $query_params['who'] = array( |
1413 $query_params['who'] = array( |
1382 'description' => __( 'Limit result set to users who are considered authors.' ), |
1414 'description' => __( 'Limit result set to users who are considered authors.' ), |