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
Fields.php
295 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 PodsForm; |
| 11 | use Pods\REST\Interfaces\Endpoints\CREATE_Interface; |
| 12 | use Pods\REST\Interfaces\Endpoints\READ_Interface; |
| 13 | use Pods\REST\Interfaces\Swagger\Provider_Interface; |
| 14 | use WP_REST_Request; |
| 15 | |
| 16 | class Fields extends Base implements READ_Interface, CREATE_Interface, Provider_Interface { |
| 17 | |
| 18 | /** |
| 19 | * {@inheritdoc} |
| 20 | * |
| 21 | * @since 2.8.0 |
| 22 | */ |
| 23 | public $route = '/fields'; |
| 24 | |
| 25 | /** |
| 26 | * {@inheritdoc} |
| 27 | * |
| 28 | * @since 2.8.0 |
| 29 | */ |
| 30 | public $object = 'field'; |
| 31 | |
| 32 | /** |
| 33 | * {@inheritdoc} |
| 34 | * |
| 35 | * @since 2.8.0 |
| 36 | */ |
| 37 | public function get_documentation() { |
| 38 | $GET_defaults = [ |
| 39 | 'in' => 'query', |
| 40 | 'default' => '', |
| 41 | ]; |
| 42 | |
| 43 | $POST_defaults = [ |
| 44 | 'in' => 'body', |
| 45 | 'default' => '', |
| 46 | 'type' => 'string', |
| 47 | ]; |
| 48 | |
| 49 | return [ |
| 50 | 'get' => [ |
| 51 | 'summary' => 'Retrieve a collection of Fields', |
| 52 | 'parameters' => $this->swaggerize_args( $this->READ_args(), $GET_defaults ), |
| 53 | 'responses' => [ |
| 54 | '200' => [ |
| 55 | 'description' => 'Returns a collection of Fields matching the request', |
| 56 | 'content' => [ |
| 57 | 'application/json' => [ |
| 58 | 'schema' => [ |
| 59 | 'type' => 'object', |
| 60 | 'properties' => [ |
| 61 | 'rest_url' => [ |
| 62 | 'type' => 'string', |
| 63 | 'format' => 'uri', |
| 64 | 'description' => __( 'This results page REST URL.', 'pods' ), |
| 65 | ], |
| 66 | 'total' => [ |
| 67 | 'type' => 'integer', |
| 68 | 'description' => __( 'The total number of results across all pages.', 'pods' ), |
| 69 | ], |
| 70 | 'total_pages' => [ |
| 71 | 'type' => 'integer', |
| 72 | 'description' => __( 'The total number of result pages matching the search criteria.', 'pods' ), |
| 73 | ], |
| 74 | 'fields' => [ |
| 75 | 'type' => 'array', |
| 76 | 'items' => [ '$ref' => '#/components/schemas/Field' ], |
| 77 | ], |
| 78 | ], |
| 79 | ], |
| 80 | ], |
| 81 | ], |
| 82 | ], |
| 83 | '400' => [ |
| 84 | 'description' => __( 'One or more of the specified query variables has a bad format.', 'pods' ), |
| 85 | 'content' => [ |
| 86 | 'application/json' => [ |
| 87 | 'schema' => [ |
| 88 | 'type' => 'object', |
| 89 | ], |
| 90 | ], |
| 91 | ], |
| 92 | ], |
| 93 | '404' => [ |
| 94 | 'description' => __( 'The requested page was not found.', 'pods' ), |
| 95 | 'content' => [ |
| 96 | 'application/json' => [ |
| 97 | 'schema' => [ |
| 98 | 'type' => 'object', |
| 99 | ], |
| 100 | ], |
| 101 | ], |
| 102 | ], |
| 103 | ], |
| 104 | ], |
| 105 | 'post' => [ |
| 106 | 'summary' => 'Create a new Field', |
| 107 | 'parameters' => $this->swaggerize_args( $this->CREATE_args(), $POST_defaults ), |
| 108 | 'responses' => [ |
| 109 | '201' => [ |
| 110 | 'description' => 'Returns the newly created Field', |
| 111 | 'content' => [ |
| 112 | 'application/json' => [ |
| 113 | 'schema' => [ |
| 114 | '$ref' => '#/components/schemas/Field', |
| 115 | ], |
| 116 | ], |
| 117 | ], |
| 118 | ], |
| 119 | '400' => [ |
| 120 | 'description' => 'The request was invalid or cannot be otherwise served', |
| 121 | 'content' => [ |
| 122 | 'application/json' => [ |
| 123 | 'schema' => [ |
| 124 | 'type' => 'object', |
| 125 | ], |
| 126 | ], |
| 127 | ], |
| 128 | ], |
| 129 | '401' => [ |
| 130 | 'description' => 'Unauthorized access - user does not have permission to create Fields', |
| 131 | 'content' => [ |
| 132 | 'application/json' => [ |
| 133 | 'schema' => [ |
| 134 | 'type' => 'object', |
| 135 | ], |
| 136 | ], |
| 137 | ], |
| 138 | ], |
| 139 | '403' => [ |
| 140 | 'description' => 'Forbidden - creation of this Field is not allowed', |
| 141 | 'content' => [ |
| 142 | 'application/json' => [ |
| 143 | 'schema' => [ |
| 144 | 'type' => 'object', |
| 145 | ], |
| 146 | ], |
| 147 | ], |
| 148 | ], |
| 149 | ], |
| 150 | ], |
| 151 | ]; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * {@inheritdoc} |
| 156 | * |
| 157 | * @since 2.8.0 |
| 158 | */ |
| 159 | public function READ_args() { |
| 160 | return [ |
| 161 | 'return_type' => [ |
| 162 | 'description' => __( 'The type of data to return.', 'pods' ), |
| 163 | 'type' => 'string', |
| 164 | 'default' => 'full', |
| 165 | 'required' => false, |
| 166 | 'enum' => [ |
| 167 | 'full', |
| 168 | 'names', |
| 169 | 'names_ids', |
| 170 | 'ids', |
| 171 | 'key_names', |
| 172 | 'count', |
| 173 | ], |
| 174 | ], |
| 175 | 'types' => [ |
| 176 | 'required' => false, |
| 177 | 'description' => __( 'A list of types to filter by.', 'pods' ), |
| 178 | 'swagger_type' => 'array', |
| 179 | 'items' => [ |
| 180 | 'type' => 'string', |
| 181 | ], |
| 182 | 'collectionFormat' => 'csv', |
| 183 | ], |
| 184 | 'ids' => [ |
| 185 | 'required' => false, |
| 186 | 'description' => __( 'A list of IDs to filter by.', 'pods' ), |
| 187 | 'swagger_type' => 'array', |
| 188 | 'items' => [ |
| 189 | 'type' => 'integer', |
| 190 | ], |
| 191 | 'collectionFormat' => 'csv', |
| 192 | ], |
| 193 | 'args' => [ |
| 194 | 'required' => false, |
| 195 | 'description' => __( 'A list of arguments to filter by.', 'pods' ), |
| 196 | 'swagger_type' => 'array', |
| 197 | ], |
| 198 | 'include_parent' => [ |
| 199 | 'type' => 'boolean', |
| 200 | 'description' => __( 'Whether to include the parent Pod details (default: off).', 'pods' ), |
| 201 | 'default' => false, |
| 202 | 'cli_boolean' => true, |
| 203 | ], |
| 204 | ]; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * {@inheritdoc} |
| 209 | * |
| 210 | * @since 2.8.0 |
| 211 | */ |
| 212 | public function get( WP_REST_Request $request ) { |
| 213 | return $this->archive_by_args( $request ); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Determine whether access to READ is available. |
| 218 | * |
| 219 | * @since 2.8.0 |
| 220 | * |
| 221 | * @return bool Whether access to READ is available. |
| 222 | */ |
| 223 | public function can_read() { |
| 224 | return pods_is_admin( 'pods' ); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * {@inheritdoc} |
| 229 | * |
| 230 | * @since 2.8.0 |
| 231 | */ |
| 232 | public function CREATE_args() { |
| 233 | return [ |
| 234 | 'pod_id' => [ |
| 235 | 'type' => 'string', |
| 236 | 'description' => __( 'The Pod ID.', 'pods' ), |
| 237 | 'validate_callback' => [ $this->validator, 'is_pod_id' ], |
| 238 | ], |
| 239 | 'pod' => [ |
| 240 | 'type' => 'string', |
| 241 | 'description' => __( 'The Pod name.', 'pods' ), |
| 242 | 'validate_callback' => [ $this->validator, 'is_pod_slug' ], |
| 243 | ], |
| 244 | 'group_id' => [ |
| 245 | 'type' => 'string', |
| 246 | 'description' => __( 'The Group ID.', 'pods' ), |
| 247 | 'validate_callback' => [ $this->validator, 'is_group_id' ], |
| 248 | ], |
| 249 | 'group' => [ |
| 250 | 'type' => 'string', |
| 251 | 'description' => __( 'The Group name.', 'pods' ), |
| 252 | 'validate_callback' => [ $this->validator, 'is_group_slug' ], |
| 253 | ], |
| 254 | 'name' => [ |
| 255 | 'type' => 'string', |
| 256 | 'description' => __( 'The name of the Field.', 'pods' ), |
| 257 | ], |
| 258 | 'label' => [ |
| 259 | 'type' => 'string', |
| 260 | 'description' => __( 'The singular label of the Field.', 'pods' ), |
| 261 | 'required' => true, |
| 262 | ], |
| 263 | 'type' => [ |
| 264 | 'type' => 'string', |
| 265 | 'description' => __( 'The type of the Field.', 'pods' ), |
| 266 | 'enum' => PodsForm::field_types_list(), |
| 267 | 'required' => true, |
| 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 create( WP_REST_REQUEST $request, $return_id = false ) { |
| 283 | return $this->create_by_args( $request, $return_id ); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * {@inheritdoc} |
| 288 | * |
| 289 | * @since 2.8.0 |
| 290 | */ |
| 291 | public function can_create() { |
| 292 | return pods_is_admin( 'pods' ); |
| 293 | } |
| 294 | } |
| 295 |