Base.php
1 week ago
Field.php
1 week ago
Field_Slug.php
1 week ago
Fields.php
1 week ago
Group.php
1 week ago
Group_Slug.php
1 week ago
Groups.php
1 week ago
Pod.php
1 week ago
Pod_Slug.php
1 week ago
Pods.php
1 week ago
Swagger_Documentation.php
1 week ago
Pods.php
275 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Pods\REST\V1\Endpoints; |
| 4 | |
| 5 | use PodsForm; |
| 6 | use Tribe__Documentation__Swagger__Provider_Interface as Swagger_Interface; |
| 7 | use Tribe__REST__Endpoints__CREATE_Endpoint_Interface as CREATE_Interface; |
| 8 | use Tribe__REST__Endpoints__READ_Endpoint_Interface as READ_Interface; |
| 9 | use WP_Error; |
| 10 | use WP_REST_Request; |
| 11 | |
| 12 | class Pods extends Base implements READ_Interface, CREATE_Interface, Swagger_Interface { |
| 13 | |
| 14 | /** |
| 15 | * {@inheritdoc} |
| 16 | * |
| 17 | * @since 2.8.0 |
| 18 | */ |
| 19 | public $route = '/pods'; |
| 20 | |
| 21 | /** |
| 22 | * {@inheritdoc} |
| 23 | * |
| 24 | * @since 2.8.0 |
| 25 | */ |
| 26 | public $object = 'pod'; |
| 27 | |
| 28 | /** |
| 29 | * {@inheritdoc} |
| 30 | * |
| 31 | * @since 2.8.0 |
| 32 | */ |
| 33 | public function get_documentation() { |
| 34 | $GET_defaults = [ |
| 35 | 'in' => 'query', |
| 36 | 'default' => '', |
| 37 | ]; |
| 38 | |
| 39 | // @todo Handle get/post |
| 40 | |
| 41 | return [ |
| 42 | 'get' => [ |
| 43 | 'parameters' => $this->swaggerize_args( $this->READ_args(), $GET_defaults ), |
| 44 | 'responses' => [ |
| 45 | '200' => [ |
| 46 | 'description' => __( 'Returns all the tickets matching the search criteria.', 'pods' ), |
| 47 | 'content' => [ |
| 48 | 'application/json' => [ |
| 49 | 'schema' => [ |
| 50 | 'type' => 'object', |
| 51 | 'properties' => [ |
| 52 | 'pods' => [ |
| 53 | 'type' => 'array', |
| 54 | 'items' => [ '$ref' => '#/components/schemas/Pod' ], |
| 55 | ], |
| 56 | ], |
| 57 | ], |
| 58 | ], |
| 59 | ], |
| 60 | ], |
| 61 | ], |
| 62 | ], |
| 63 | ]; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * {@inheritdoc} |
| 68 | * |
| 69 | * @since 2.8.0 |
| 70 | */ |
| 71 | public function READ_args() { |
| 72 | return [ |
| 73 | 'return_type' => [ |
| 74 | 'description' => __( 'The type of data to return.', 'pods' ), |
| 75 | 'type' => 'string', |
| 76 | 'default' => 'full', |
| 77 | 'required' => false, |
| 78 | 'enum' => [ |
| 79 | 'full', |
| 80 | 'names', |
| 81 | 'ids', |
| 82 | 'count', |
| 83 | ], |
| 84 | ], |
| 85 | 'types' => [ |
| 86 | 'required' => false, |
| 87 | 'description' => __( 'A list of types to filter by.', 'pods' ), |
| 88 | 'swagger_type' => 'array', |
| 89 | 'items' => [ |
| 90 | 'type' => 'string', |
| 91 | ], |
| 92 | 'collectionFormat' => 'csv', |
| 93 | ], |
| 94 | 'ids' => [ |
| 95 | 'required' => false, |
| 96 | 'description' => __( 'A list of IDs to filter by.', 'pods' ), |
| 97 | 'swagger_type' => 'array', |
| 98 | 'items' => [ |
| 99 | 'type' => 'integer', |
| 100 | ], |
| 101 | 'collectionFormat' => 'csv', |
| 102 | ], |
| 103 | 'args' => [ |
| 104 | 'required' => false, |
| 105 | 'description' => __( 'A list of arguments to filter by.', 'pods' ), |
| 106 | 'swagger_type' => 'array', |
| 107 | ], |
| 108 | ]; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * {@inheritdoc} |
| 113 | * |
| 114 | * @since 2.8.0 |
| 115 | */ |
| 116 | public function get( WP_REST_Request $request ) { |
| 117 | return $this->archive_by_args( $request ); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Determine whether access to READ is available. |
| 122 | * |
| 123 | * @since 2.8.0 |
| 124 | * |
| 125 | * @return bool Whether access to READ is available. |
| 126 | */ |
| 127 | public function can_read() { |
| 128 | return pods_is_admin( 'pods' ); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * {@inheritdoc} |
| 133 | * |
| 134 | * @since 2.8.0 |
| 135 | */ |
| 136 | public function CREATE_args() { |
| 137 | return [ |
| 138 | 'mode' => [ |
| 139 | 'type' => 'string', |
| 140 | 'description' => __( 'The mode for creating the Pod.', 'pods' ), |
| 141 | 'default' => 'create', |
| 142 | 'enum' => [ |
| 143 | 'create', |
| 144 | 'extend', |
| 145 | ], |
| 146 | ], |
| 147 | 'name' => [ |
| 148 | 'type' => 'string', |
| 149 | 'description' => __( 'The name of the Pod.', 'pods' ), |
| 150 | ], |
| 151 | 'label' => [ |
| 152 | 'type' => 'string', |
| 153 | 'description' => __( 'The plural label of the Pod.', 'pods' ), |
| 154 | ], |
| 155 | 'type' => [ |
| 156 | 'type' => 'string', |
| 157 | 'description' => __( 'The type of the Pod.', 'pods' ), |
| 158 | 'enum' => PodsForm::pod_types_list(), |
| 159 | 'required' => true, |
| 160 | ], |
| 161 | 'storage' => [ |
| 162 | 'type' => 'string', |
| 163 | 'description' => __( 'The storage used for the Pod.', 'pods' ), |
| 164 | 'default' => 'meta', |
| 165 | 'enum' => [ |
| 166 | 'meta', |
| 167 | 'table', |
| 168 | 'none', |
| 169 | ], |
| 170 | ], |
| 171 | 'label_singular' => [ |
| 172 | 'type' => 'string', |
| 173 | 'description' => __( 'The singular label of the Pod.', 'pods' ), |
| 174 | ], |
| 175 | 'menu_name' => [ |
| 176 | 'type' => 'string', |
| 177 | 'description' => __( 'The menu label of the Pod.', 'pods' ), |
| 178 | ], |
| 179 | 'menu_location' => [ |
| 180 | 'type' => 'string', |
| 181 | 'description' => __( 'The menu location of the Pod.', 'pods' ), |
| 182 | ], |
| 183 | ]; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * {@inheritdoc} |
| 188 | * |
| 189 | * @since 2.8.0 |
| 190 | */ |
| 191 | public function create( WP_REST_REQUEST $request, $return_id = false ) { |
| 192 | if ( ! empty( $request['groups'] ) ) { |
| 193 | $request->set_param( 'groups', null ); |
| 194 | } |
| 195 | |
| 196 | if ( ! empty( $request['fields'] ) ) { |
| 197 | $request->set_param( 'fields', null ); |
| 198 | } |
| 199 | |
| 200 | $mode = $request->get_param( 'mode' ); |
| 201 | $type = $request->get_param( 'type' ); |
| 202 | $storage = $request->get_param( 'storage' ); |
| 203 | |
| 204 | if ( 'extend' === $mode ) { |
| 205 | $params = [ |
| 206 | 'create_extend' => 'extend', |
| 207 | 'extend_pod_type' => $type, |
| 208 | 'extend_storage' => $storage, |
| 209 | ]; |
| 210 | |
| 211 | $name = $request->get_param( 'name' ); |
| 212 | |
| 213 | if ( 'post_type' === $params['extend_pod_type'] ) { |
| 214 | $params['extend_post_type'] = $name; |
| 215 | } elseif ( 'taxonomy' === $params['extend_pod_type'] ) { |
| 216 | $params['extend_taxonomy'] = $name; |
| 217 | } elseif ( 'table' === $params['extend_pod_type'] ) { |
| 218 | $params['extend_table'] = $name; |
| 219 | } elseif ( in_array( $params['extend_pod_type'], [ 'pod', 'settings' ], true ) ) { |
| 220 | return new WP_Error( 'rest-object-extend-type-not-supported', __( 'Pod type not supported for extending.', 'pods' ) ); |
| 221 | } |
| 222 | } else { |
| 223 | $params = [ |
| 224 | 'create_extend' => 'create', |
| 225 | 'create_pod_type' => $type, |
| 226 | 'create_storage' => $storage, |
| 227 | 'create_label_plural' => $request->get_param( 'label' ), |
| 228 | 'create_label_singular' => $request->get_param( 'label_singular' ), |
| 229 | 'create_label_menu' => $request->get_param( 'menu_name' ), |
| 230 | 'create_menu_location' => $request->get_param( 'menu_location' ), |
| 231 | ]; |
| 232 | |
| 233 | if ( 'settings' === $params['create_pod_type'] ) { |
| 234 | $params['create_label_title'] = $params['create_label_plural']; |
| 235 | } |
| 236 | |
| 237 | $name = $request->get_param( 'name' ); |
| 238 | |
| 239 | if ( 'settings' === $params['create_pod_type'] ) { |
| 240 | $params['create_setting_name'] = $name; |
| 241 | $params['create_storage'] = 'none'; |
| 242 | } else { |
| 243 | $params['create_name'] = $name; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | $api = pods_api(); |
| 248 | |
| 249 | $api->display_errors = 'wp_error'; |
| 250 | |
| 251 | $id = $api->add_pod( $params ); |
| 252 | |
| 253 | if ( is_wp_error( $id ) ) { |
| 254 | return $id; |
| 255 | } |
| 256 | |
| 257 | if ( empty( $id ) ) { |
| 258 | return new WP_Error( 'rest-object-not-added', sprintf( __( '%s not added.', 'pods' ), ucwords( $this->object ) ) ); |
| 259 | } |
| 260 | |
| 261 | return $this->get_by_args( [ |
| 262 | 'id' => $id, |
| 263 | ], 'id', null, $return_id ); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * {@inheritdoc} |
| 268 | * |
| 269 | * @since 2.8.0 |
| 270 | */ |
| 271 | public function can_create() { |
| 272 | return pods_is_admin( 'pods' ); |
| 273 | } |
| 274 | } |
| 275 |