Base.php
4 months ago
Field.php
4 months ago
Field_Slug.php
4 months ago
Fields.php
4 months ago
Group.php
4 months ago
Group_Slug.php
4 months ago
Groups.php
4 months ago
Pod.php
4 months ago
Pod_Slug.php
4 months ago
Pods.php
4 months ago
Swagger_Documentation.php
4 months ago
Field.php
330 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Pods\REST\V1\Endpoints; |
| 4 | |
| 5 | // Don't load directly. |
| 6 | if ( ! defined( 'ABSPATH' ) ) { |
| 7 | die( '-1' ); |
| 8 | } |
| 9 | |
| 10 | use Pods\REST\Interfaces\Endpoints\DELETE_Interface; |
| 11 | use Pods\REST\Interfaces\Endpoints\READ_Interface; |
| 12 | use Pods\REST\Interfaces\Endpoints\UPDATE_Interface; |
| 13 | use Pods\REST\Interfaces\Swagger\Provider_Interface; |
| 14 | use WP_REST_Request; |
| 15 | |
| 16 | class Field extends Base implements READ_Interface, UPDATE_Interface, DELETE_Interface, Provider_Interface { |
| 17 | |
| 18 | /** |
| 19 | * {@inheritdoc} |
| 20 | * |
| 21 | * @since 2.8.0 |
| 22 | */ |
| 23 | public $route = '/fields/%1$d'; |
| 24 | |
| 25 | /** |
| 26 | * {@inheritdoc} |
| 27 | * |
| 28 | * @since 2.8.11 |
| 29 | */ |
| 30 | public $rest_route = '/fields/(?P<id>\\d+)'; |
| 31 | |
| 32 | /** |
| 33 | * {@inheritdoc} |
| 34 | * |
| 35 | * @since 2.8.11 |
| 36 | */ |
| 37 | public $rest_doc_route = '/fields/{id}'; |
| 38 | |
| 39 | /** |
| 40 | * {@inheritdoc} |
| 41 | * |
| 42 | * @since 2.8.0 |
| 43 | */ |
| 44 | public $object = 'field'; |
| 45 | |
| 46 | /** |
| 47 | * {@inheritdoc} |
| 48 | * |
| 49 | * @since 2.8.0 |
| 50 | */ |
| 51 | public function get_documentation() { |
| 52 | $GET_defaults = [ |
| 53 | 'in' => 'query', |
| 54 | 'default' => '', |
| 55 | 'type' => 'string', |
| 56 | ]; |
| 57 | |
| 58 | $POST_defaults = [ |
| 59 | 'in' => 'body', |
| 60 | 'default' => '', |
| 61 | 'type' => 'string', |
| 62 | ]; |
| 63 | |
| 64 | return [ |
| 65 | 'get' => [ |
| 66 | 'summary' => 'Retrieve a specific Field', |
| 67 | 'parameters' => $this->swaggerize_args( $this->READ_args(), $GET_defaults ), |
| 68 | 'responses' => [ |
| 69 | '200' => [ |
| 70 | 'description' => 'Returns the requested Field with its details', |
| 71 | 'content' => [ |
| 72 | 'application/json' => [ |
| 73 | 'schema' => [ |
| 74 | '$ref' => '#/components/schemas/Field', |
| 75 | ], |
| 76 | ], |
| 77 | ], |
| 78 | ], |
| 79 | '400' => [ |
| 80 | 'description' => 'The request was invalid or cannot be otherwise served', |
| 81 | 'content' => [ |
| 82 | 'application/json' => [ |
| 83 | 'schema' => [ |
| 84 | 'type' => 'object', |
| 85 | ], |
| 86 | ], |
| 87 | ], |
| 88 | ], |
| 89 | '401' => [ |
| 90 | 'description' => 'Unauthorized access - user does not have permission to access this Field', |
| 91 | 'content' => [ |
| 92 | 'application/json' => [ |
| 93 | 'schema' => [ |
| 94 | 'type' => 'object', |
| 95 | ], |
| 96 | ], |
| 97 | ], |
| 98 | ], |
| 99 | '404' => [ |
| 100 | 'description' => 'The requested Field was not found', |
| 101 | 'content' => [ |
| 102 | 'application/json' => [ |
| 103 | 'schema' => [ |
| 104 | 'type' => 'object', |
| 105 | ], |
| 106 | ], |
| 107 | ], |
| 108 | ], |
| 109 | ], |
| 110 | ], |
| 111 | 'put' => [ |
| 112 | 'summary' => 'Update a specific Field', |
| 113 | 'parameters' => $this->swaggerize_args( $this->EDIT_args(), $POST_defaults ), |
| 114 | 'responses' => [ |
| 115 | '200' => [ |
| 116 | 'description' => 'Returns the updated Field', |
| 117 | 'content' => [ |
| 118 | 'application/json' => [ |
| 119 | 'schema' => [ |
| 120 | '$ref' => '#/components/schemas/Field', |
| 121 | ], |
| 122 | ], |
| 123 | ], |
| 124 | ], |
| 125 | '400' => [ |
| 126 | 'description' => 'The request was invalid or cannot be otherwise served', |
| 127 | 'content' => [ |
| 128 | 'application/json' => [ |
| 129 | 'schema' => [ |
| 130 | 'type' => 'object', |
| 131 | ], |
| 132 | ], |
| 133 | ], |
| 134 | ], |
| 135 | '401' => [ |
| 136 | 'description' => 'Unauthorized access - user does not have permission to update this Field', |
| 137 | 'content' => [ |
| 138 | 'application/json' => [ |
| 139 | 'schema' => [ |
| 140 | 'type' => 'object', |
| 141 | ], |
| 142 | ], |
| 143 | ], |
| 144 | ], |
| 145 | '404' => [ |
| 146 | 'description' => 'The Field to update was not found', |
| 147 | 'content' => [ |
| 148 | 'application/json' => [ |
| 149 | 'schema' => [ |
| 150 | 'type' => 'object', |
| 151 | ], |
| 152 | ], |
| 153 | ], |
| 154 | ], |
| 155 | ], |
| 156 | ], |
| 157 | 'delete' => [ |
| 158 | 'summary' => 'Delete a specific Field', |
| 159 | 'parameters' => $this->swaggerize_args( $this->DELETE_args(), $POST_defaults ), |
| 160 | 'responses' => [ |
| 161 | '200' => [ |
| 162 | 'description' => 'Returns the deleted Field details', |
| 163 | 'content' => [ |
| 164 | 'application/json' => [ |
| 165 | 'schema' => [ |
| 166 | '$ref' => '#/components/schemas/Field', |
| 167 | ], |
| 168 | ], |
| 169 | ], |
| 170 | ], |
| 171 | '400' => [ |
| 172 | 'description' => 'The request was invalid or cannot be otherwise served', |
| 173 | 'content' => [ |
| 174 | 'application/json' => [ |
| 175 | 'schema' => [ |
| 176 | 'type' => 'object', |
| 177 | ], |
| 178 | ], |
| 179 | ], |
| 180 | ], |
| 181 | '401' => [ |
| 182 | 'description' => 'Unauthorized access - user does not have permission to delete this Field', |
| 183 | 'content' => [ |
| 184 | 'application/json' => [ |
| 185 | 'schema' => [ |
| 186 | 'type' => 'object', |
| 187 | ], |
| 188 | ], |
| 189 | ], |
| 190 | ], |
| 191 | '404' => [ |
| 192 | 'description' => 'The Field to delete was not found', |
| 193 | 'content' => [ |
| 194 | 'application/json' => [ |
| 195 | 'schema' => [ |
| 196 | 'type' => 'object', |
| 197 | ], |
| 198 | ], |
| 199 | ], |
| 200 | ], |
| 201 | ], |
| 202 | ], |
| 203 | ]; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * {@inheritdoc} |
| 208 | * |
| 209 | * @since 2.8.0 |
| 210 | */ |
| 211 | public function READ_args() { |
| 212 | return [ |
| 213 | 'id' => [ |
| 214 | 'type' => 'integer', |
| 215 | 'in' => 'path', |
| 216 | 'description' => __( 'The Field ID.', 'pods' ), |
| 217 | 'required' => true, |
| 218 | 'validate_callback' => [ $this->validator, 'is_field_id' ], |
| 219 | ], |
| 220 | ]; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * {@inheritdoc} |
| 225 | * |
| 226 | * @since 2.8.0 |
| 227 | */ |
| 228 | public function get( WP_REST_Request $request ) { |
| 229 | return $this->get_by_args( 'id', 'id', $request ); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Determine whether access to READ is available. |
| 234 | * |
| 235 | * @since 2.8.0 |
| 236 | * |
| 237 | * @return bool Whether access to READ is available. |
| 238 | */ |
| 239 | public function can_read() { |
| 240 | return pods_is_admin( 'pods' ); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * {@inheritdoc} |
| 245 | * |
| 246 | * @since 2.8.0 |
| 247 | */ |
| 248 | public function EDIT_args() { |
| 249 | return [ |
| 250 | 'id' => [ |
| 251 | 'type' => 'integer', |
| 252 | 'in' => 'path', |
| 253 | 'description' => __( 'The Field ID.', 'pods' ), |
| 254 | 'required' => true, |
| 255 | 'validate_callback' => [ $this->validator, 'is_field_id' ], |
| 256 | ], |
| 257 | 'name' => [ |
| 258 | 'type' => 'string', |
| 259 | 'description' => __( 'The new name of the Field.', 'pods' ), |
| 260 | ], |
| 261 | 'label' => [ |
| 262 | 'type' => 'string', |
| 263 | 'description' => __( 'The singular label of the Field.', 'pods' ), |
| 264 | ], |
| 265 | 'type' => [ |
| 266 | 'type' => 'string', |
| 267 | 'description' => __( 'The type of the Field.', 'pods' ), |
| 268 | ], |
| 269 | 'args' => [ |
| 270 | 'required' => false, |
| 271 | 'description' => __( 'A list of additional options to save to the Field.', 'pods' ), |
| 272 | 'swagger_type' => 'array', |
| 273 | ], |
| 274 | ]; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * {@inheritdoc} |
| 279 | * |
| 280 | * @since 2.8.0 |
| 281 | */ |
| 282 | public function update( WP_REST_Request $request ) { |
| 283 | return $this->update_by_args( 'id', 'id', $request ); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * {@inheritdoc} |
| 288 | * |
| 289 | * @since 2.8.0 |
| 290 | */ |
| 291 | public function can_edit() { |
| 292 | return pods_is_admin( 'pods' ); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * {@inheritdoc} |
| 297 | * |
| 298 | * @since 2.8.0 |
| 299 | */ |
| 300 | public function DELETE_args() { |
| 301 | return [ |
| 302 | 'id' => [ |
| 303 | 'type' => 'integer', |
| 304 | 'in' => 'path', |
| 305 | 'description' => __( 'The Field ID.', 'pods' ), |
| 306 | 'required' => true, |
| 307 | 'validate_callback' => [ $this->validator, 'is_field_id' ], |
| 308 | ], |
| 309 | ]; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * {@inheritdoc} |
| 314 | * |
| 315 | * @since 2.8.0 |
| 316 | */ |
| 317 | public function delete( WP_REST_Request $request ) { |
| 318 | return $this->delete_by_args( 'id', 'id', $request ); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * {@inheritdoc} |
| 323 | * |
| 324 | * @since 2.8.0 |
| 325 | */ |
| 326 | public function can_delete() { |
| 327 | return pods_is_admin( 'pods' ); |
| 328 | } |
| 329 | } |
| 330 |