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