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