Base.php
1 year ago
Field.php
1 year ago
Field_Slug.php
4 years ago
Fields.php
1 year ago
Group.php
1 year ago
Group_Slug.php
4 years ago
Groups.php
1 year ago
Pod.php
1 year ago
Pod_Slug.php
4 years ago
Pods.php
1 year ago
Swagger_Documentation.php
1 year ago
Pods.php
346 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Pods\REST\V1\Endpoints; |
| 4 | |
| 5 | use Pods\REST\Interfaces\Endpoints\CREATE_Interface; |
| 6 | use Pods\REST\Interfaces\Endpoints\READ_Interface; |
| 7 | use Pods\REST\Interfaces\Swagger\Provider_Interface; |
| 8 | use WP_Error; |
| 9 | use WP_REST_Request; |
| 10 | |
| 11 | class Pods extends Base implements READ_Interface, CREATE_Interface, Provider_Interface { |
| 12 | |
| 13 | /** |
| 14 | * {@inheritdoc} |
| 15 | * |
| 16 | * @since 2.8.0 |
| 17 | */ |
| 18 | public $route = '/pods'; |
| 19 | |
| 20 | /** |
| 21 | * {@inheritdoc} |
| 22 | * |
| 23 | * @since 2.8.0 |
| 24 | */ |
| 25 | public $object = 'pod'; |
| 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 | 'type' => 'string', |
| 37 | ]; |
| 38 | |
| 39 | $POST_defaults = [ |
| 40 | 'in' => 'body', |
| 41 | 'default' => '', |
| 42 | 'type' => 'string', |
| 43 | ]; |
| 44 | |
| 45 | return [ |
| 46 | 'get' => [ |
| 47 | 'summary' => 'Retrieve a collection of Pods', |
| 48 | 'parameters' => $this->swaggerize_args( $this->READ_args(), $GET_defaults ), |
| 49 | 'responses' => [ |
| 50 | '200' => [ |
| 51 | 'description' => 'Returns a collection of Pods matching the request', |
| 52 | 'content' => [ |
| 53 | 'application/json' => [ |
| 54 | 'schema' => [ |
| 55 | 'type' => 'object', |
| 56 | 'properties' => [ |
| 57 | 'pods' => [ |
| 58 | 'type' => 'array', |
| 59 | 'items' => [ '$ref' => '#/components/schemas/Pod' ], |
| 60 | ], |
| 61 | ], |
| 62 | ], |
| 63 | ], |
| 64 | ], |
| 65 | ], |
| 66 | '400' => [ |
| 67 | 'description' => 'The request was invalid or cannot be otherwise served', |
| 68 | 'content' => [ |
| 69 | 'application/json' => [ |
| 70 | 'schema' => [ |
| 71 | 'type' => 'object', |
| 72 | ], |
| 73 | ], |
| 74 | ], |
| 75 | ], |
| 76 | '401' => [ |
| 77 | 'description' => 'Unauthorized access - user does not have permission to access Pods', |
| 78 | 'content' => [ |
| 79 | 'application/json' => [ |
| 80 | 'schema' => [ |
| 81 | 'type' => 'object', |
| 82 | ], |
| 83 | ], |
| 84 | ], |
| 85 | ], |
| 86 | ], |
| 87 | ], |
| 88 | 'post' => [ |
| 89 | 'summary' => 'Create a new Pod', |
| 90 | 'parameters' => $this->swaggerize_args( $this->CREATE_args(), $POST_defaults ), |
| 91 | 'responses' => [ |
| 92 | '201' => [ |
| 93 | 'description' => 'Returns the newly created Pod', |
| 94 | 'content' => [ |
| 95 | 'application/json' => [ |
| 96 | 'schema' => [ |
| 97 | '$ref' => '#/components/schemas/Pod', |
| 98 | ], |
| 99 | ], |
| 100 | ], |
| 101 | ], |
| 102 | '400' => [ |
| 103 | 'description' => 'The request was invalid or cannot be otherwise served', |
| 104 | 'content' => [ |
| 105 | 'application/json' => [ |
| 106 | 'schema' => [ |
| 107 | 'type' => 'object', |
| 108 | ], |
| 109 | ], |
| 110 | ], |
| 111 | ], |
| 112 | '401' => [ |
| 113 | 'description' => 'Unauthorized access - user does not have permission to create Pods', |
| 114 | 'content' => [ |
| 115 | 'application/json' => [ |
| 116 | 'schema' => [ |
| 117 | 'type' => 'object', |
| 118 | ], |
| 119 | ], |
| 120 | ], |
| 121 | ], |
| 122 | '403' => [ |
| 123 | 'description' => 'Forbidden - creation of this Pod is not allowed', |
| 124 | 'content' => [ |
| 125 | 'application/json' => [ |
| 126 | 'schema' => [ |
| 127 | 'type' => 'object', |
| 128 | ], |
| 129 | ], |
| 130 | ], |
| 131 | ], |
| 132 | ], |
| 133 | ], |
| 134 | ]; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * {@inheritdoc} |
| 139 | * |
| 140 | * @since 2.8.0 |
| 141 | */ |
| 142 | public function READ_args() { |
| 143 | return [ |
| 144 | 'return_type' => [ |
| 145 | 'description' => __( 'The type of data to return.', 'pods' ), |
| 146 | 'type' => 'string', |
| 147 | 'default' => 'full', |
| 148 | 'required' => false, |
| 149 | 'enum' => [ |
| 150 | 'full', |
| 151 | 'names', |
| 152 | 'ids', |
| 153 | 'count', |
| 154 | ], |
| 155 | ], |
| 156 | 'types' => [ |
| 157 | 'required' => false, |
| 158 | 'description' => __( 'A list of types to filter by.', 'pods' ), |
| 159 | 'swagger_type' => 'array', |
| 160 | 'items' => [ |
| 161 | 'type' => 'string', |
| 162 | ], |
| 163 | 'collectionFormat' => 'csv', |
| 164 | ], |
| 165 | 'ids' => [ |
| 166 | 'required' => false, |
| 167 | 'description' => __( 'A list of IDs to filter by.', 'pods' ), |
| 168 | 'swagger_type' => 'array', |
| 169 | 'items' => [ |
| 170 | 'type' => 'integer', |
| 171 | ], |
| 172 | 'collectionFormat' => 'csv', |
| 173 | ], |
| 174 | 'args' => [ |
| 175 | 'required' => false, |
| 176 | 'description' => __( 'A list of arguments to filter by.', 'pods' ), |
| 177 | 'swagger_type' => 'array', |
| 178 | ], |
| 179 | ]; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * {@inheritdoc} |
| 184 | * |
| 185 | * @since 2.8.0 |
| 186 | */ |
| 187 | public function get( WP_REST_Request $request ) { |
| 188 | return $this->archive_by_args( $request ); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Determine whether access to READ is available. |
| 193 | * |
| 194 | * @since 2.8.0 |
| 195 | * |
| 196 | * @return bool Whether access to READ is available. |
| 197 | */ |
| 198 | public function can_read() { |
| 199 | return pods_is_admin( 'pods' ); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * {@inheritdoc} |
| 204 | * |
| 205 | * @since 2.8.0 |
| 206 | */ |
| 207 | public function CREATE_args() { |
| 208 | return [ |
| 209 | 'mode' => [ |
| 210 | 'type' => 'string', |
| 211 | 'description' => __( 'The mode for creating the Pod.', 'pods' ), |
| 212 | 'default' => 'create', |
| 213 | 'enum' => [ |
| 214 | 'create', |
| 215 | 'extend', |
| 216 | ], |
| 217 | ], |
| 218 | 'name' => [ |
| 219 | 'type' => 'string', |
| 220 | 'description' => __( 'The name of the Pod.', 'pods' ), |
| 221 | ], |
| 222 | 'label' => [ |
| 223 | 'type' => 'string', |
| 224 | 'description' => __( 'The plural label of the Pod.', 'pods' ), |
| 225 | ], |
| 226 | 'type' => [ |
| 227 | 'type' => 'string', |
| 228 | 'description' => __( 'The type of the Pod.', 'pods' ), |
| 229 | 'enum' => array_keys( pods_api()->get_pod_types() ), |
| 230 | 'required' => true, |
| 231 | ], |
| 232 | 'storage' => [ |
| 233 | 'type' => 'string', |
| 234 | 'description' => __( 'The storage used for the Pod.', 'pods' ), |
| 235 | 'default' => 'meta', |
| 236 | 'enum' => [ |
| 237 | 'meta', |
| 238 | 'table', |
| 239 | 'none', |
| 240 | ], |
| 241 | ], |
| 242 | 'label_singular' => [ |
| 243 | 'type' => 'string', |
| 244 | 'description' => __( 'The singular label of the Pod.', 'pods' ), |
| 245 | ], |
| 246 | 'menu_name' => [ |
| 247 | 'type' => 'string', |
| 248 | 'description' => __( 'The menu label of the Pod.', 'pods' ), |
| 249 | ], |
| 250 | 'menu_location' => [ |
| 251 | 'type' => 'string', |
| 252 | 'description' => __( 'The menu location of the Pod.', 'pods' ), |
| 253 | ], |
| 254 | ]; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * {@inheritdoc} |
| 259 | * |
| 260 | * @since 2.8.0 |
| 261 | */ |
| 262 | public function create( WP_REST_REQUEST $request, $return_id = false ) { |
| 263 | if ( ! empty( $request['groups'] ) ) { |
| 264 | $request->set_param( 'groups', null ); |
| 265 | } |
| 266 | |
| 267 | if ( ! empty( $request['fields'] ) ) { |
| 268 | $request->set_param( 'fields', null ); |
| 269 | } |
| 270 | |
| 271 | $mode = $request->get_param( 'mode' ); |
| 272 | $type = $request->get_param( 'type' ); |
| 273 | $storage = $request->get_param( 'storage' ); |
| 274 | |
| 275 | if ( 'extend' === $mode ) { |
| 276 | $params = [ |
| 277 | 'create_extend' => 'extend', |
| 278 | 'extend_pod_type' => $type, |
| 279 | 'extend_storage' => $storage, |
| 280 | ]; |
| 281 | |
| 282 | $name = $request->get_param( 'name' ); |
| 283 | |
| 284 | if ( 'post_type' === $params['extend_pod_type'] ) { |
| 285 | $params['extend_post_type'] = $name; |
| 286 | } elseif ( 'taxonomy' === $params['extend_pod_type'] ) { |
| 287 | $params['extend_taxonomy'] = $name; |
| 288 | } elseif ( 'table' === $params['extend_pod_type'] ) { |
| 289 | $params['extend_table'] = $name; |
| 290 | } elseif ( in_array( $params['extend_pod_type'], [ 'pod', 'settings' ], true ) ) { |
| 291 | return new WP_Error( 'rest-object-extend-type-not-supported', __( 'Pod type not supported for extending.', 'pods' ) ); |
| 292 | } |
| 293 | } else { |
| 294 | $params = [ |
| 295 | 'create_extend' => 'create', |
| 296 | 'create_pod_type' => $type, |
| 297 | 'create_storage' => $storage, |
| 298 | 'create_label_plural' => $request->get_param( 'label' ), |
| 299 | 'create_label_singular' => $request->get_param( 'label_singular' ), |
| 300 | 'create_label_menu' => $request->get_param( 'menu_name' ), |
| 301 | 'create_menu_location' => $request->get_param( 'menu_location' ), |
| 302 | ]; |
| 303 | |
| 304 | if ( 'settings' === $params['create_pod_type'] ) { |
| 305 | $params['create_label_title'] = $params['create_label_plural']; |
| 306 | } |
| 307 | |
| 308 | $name = $request->get_param( 'name' ); |
| 309 | |
| 310 | if ( 'settings' === $params['create_pod_type'] ) { |
| 311 | $params['create_setting_name'] = $name; |
| 312 | $params['create_storage'] = 'none'; |
| 313 | } else { |
| 314 | $params['create_name'] = $name; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | $api = pods_api(); |
| 319 | |
| 320 | $api->display_errors = 'wp_error'; |
| 321 | |
| 322 | $id = $api->add_pod( $params ); |
| 323 | |
| 324 | if ( is_wp_error( $id ) ) { |
| 325 | return $id; |
| 326 | } |
| 327 | |
| 328 | if ( empty( $id ) ) { |
| 329 | return new WP_Error( 'rest-object-not-added', sprintf( __( '%s not added.', 'pods' ), ucwords( $this->object ) ) ); |
| 330 | } |
| 331 | |
| 332 | return $this->get_by_args( [ |
| 333 | 'id' => $id, |
| 334 | ], 'id', null, $return_id ); |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * {@inheritdoc} |
| 339 | * |
| 340 | * @since 2.8.0 |
| 341 | */ |
| 342 | public function can_create() { |
| 343 | return pods_is_admin( 'pods' ); |
| 344 | } |
| 345 | } |
| 346 |