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
Pods.php
274 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 | ]; |
| 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 | 'pods' => [ |
| 52 | 'type' => 'array', |
| 53 | 'items' => [ '$ref' => '#/components/schemas/Pod' ], |
| 54 | ], |
| 55 | ], |
| 56 | ], |
| 57 | ], |
| 58 | ], |
| 59 | ], |
| 60 | ], |
| 61 | ], |
| 62 | ]; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * {@inheritdoc} |
| 67 | * |
| 68 | * @since 2.8.0 |
| 69 | */ |
| 70 | public function READ_args() { |
| 71 | return [ |
| 72 | 'return_type' => [ |
| 73 | 'description' => __( 'The type of data to return.', 'pods' ), |
| 74 | 'type' => 'string', |
| 75 | 'default' => 'full', |
| 76 | 'required' => false, |
| 77 | 'enum' => [ |
| 78 | 'full', |
| 79 | 'names', |
| 80 | 'ids', |
| 81 | 'count', |
| 82 | ], |
| 83 | ], |
| 84 | 'types' => [ |
| 85 | 'required' => false, |
| 86 | 'description' => __( 'A list of types to filter by.', 'pods' ), |
| 87 | 'swagger_type' => 'array', |
| 88 | 'items' => [ |
| 89 | 'type' => 'string', |
| 90 | ], |
| 91 | 'collectionFormat' => 'csv', |
| 92 | ], |
| 93 | 'ids' => [ |
| 94 | 'required' => false, |
| 95 | 'description' => __( 'A list of IDs to filter by.', 'pods' ), |
| 96 | 'swagger_type' => 'array', |
| 97 | 'items' => [ |
| 98 | 'type' => 'integer', |
| 99 | ], |
| 100 | 'collectionFormat' => 'csv', |
| 101 | ], |
| 102 | 'args' => [ |
| 103 | 'required' => false, |
| 104 | 'description' => __( 'A list of arguments to filter by.', 'pods' ), |
| 105 | 'swagger_type' => 'array', |
| 106 | ], |
| 107 | ]; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * {@inheritdoc} |
| 112 | * |
| 113 | * @since 2.8.0 |
| 114 | */ |
| 115 | public function get( WP_REST_Request $request ) { |
| 116 | return $this->archive_by_args( $request ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Determine whether access to READ is available. |
| 121 | * |
| 122 | * @since 2.8.0 |
| 123 | * |
| 124 | * @return bool Whether access to READ is available. |
| 125 | */ |
| 126 | public function can_read() { |
| 127 | return pods_is_admin( 'pods' ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * {@inheritdoc} |
| 132 | * |
| 133 | * @since 2.8.0 |
| 134 | */ |
| 135 | public function CREATE_args() { |
| 136 | return [ |
| 137 | 'mode' => [ |
| 138 | 'type' => 'string', |
| 139 | 'description' => __( 'The mode for creating the Pod.', 'pods' ), |
| 140 | 'default' => 'create', |
| 141 | 'enum' => [ |
| 142 | 'create', |
| 143 | 'extend', |
| 144 | ], |
| 145 | ], |
| 146 | 'name' => [ |
| 147 | 'type' => 'string', |
| 148 | 'description' => __( 'The name of the Pod.', 'pods' ), |
| 149 | ], |
| 150 | 'label' => [ |
| 151 | 'type' => 'string', |
| 152 | 'description' => __( 'The plural label of the Pod.', 'pods' ), |
| 153 | ], |
| 154 | 'type' => [ |
| 155 | 'type' => 'string', |
| 156 | 'description' => __( 'The type of the Pod.', 'pods' ), |
| 157 | 'enum' => array_keys( pods_api()->get_pod_types() ), |
| 158 | 'required' => true, |
| 159 | ], |
| 160 | 'storage' => [ |
| 161 | 'type' => 'string', |
| 162 | 'description' => __( 'The storage used for the Pod.', 'pods' ), |
| 163 | 'default' => 'meta', |
| 164 | 'enum' => [ |
| 165 | 'meta', |
| 166 | 'table', |
| 167 | 'none', |
| 168 | ], |
| 169 | ], |
| 170 | 'label_singular' => [ |
| 171 | 'type' => 'string', |
| 172 | 'description' => __( 'The singular label of the Pod.', 'pods' ), |
| 173 | ], |
| 174 | 'menu_name' => [ |
| 175 | 'type' => 'string', |
| 176 | 'description' => __( 'The menu label of the Pod.', 'pods' ), |
| 177 | ], |
| 178 | 'menu_location' => [ |
| 179 | 'type' => 'string', |
| 180 | 'description' => __( 'The menu location of the Pod.', 'pods' ), |
| 181 | ], |
| 182 | ]; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * {@inheritdoc} |
| 187 | * |
| 188 | * @since 2.8.0 |
| 189 | */ |
| 190 | public function create( WP_REST_REQUEST $request, $return_id = false ) { |
| 191 | if ( ! empty( $request['groups'] ) ) { |
| 192 | $request->set_param( 'groups', null ); |
| 193 | } |
| 194 | |
| 195 | if ( ! empty( $request['fields'] ) ) { |
| 196 | $request->set_param( 'fields', null ); |
| 197 | } |
| 198 | |
| 199 | $mode = $request->get_param( 'mode' ); |
| 200 | $type = $request->get_param( 'type' ); |
| 201 | $storage = $request->get_param( 'storage' ); |
| 202 | |
| 203 | if ( 'extend' === $mode ) { |
| 204 | $params = [ |
| 205 | 'create_extend' => 'extend', |
| 206 | 'extend_pod_type' => $type, |
| 207 | 'extend_storage' => $storage, |
| 208 | ]; |
| 209 | |
| 210 | $name = $request->get_param( 'name' ); |
| 211 | |
| 212 | if ( 'post_type' === $params['extend_pod_type'] ) { |
| 213 | $params['extend_post_type'] = $name; |
| 214 | } elseif ( 'taxonomy' === $params['extend_pod_type'] ) { |
| 215 | $params['extend_taxonomy'] = $name; |
| 216 | } elseif ( 'table' === $params['extend_pod_type'] ) { |
| 217 | $params['extend_table'] = $name; |
| 218 | } elseif ( in_array( $params['extend_pod_type'], [ 'pod', 'settings' ], true ) ) { |
| 219 | return new WP_Error( 'rest-object-extend-type-not-supported', __( 'Pod type not supported for extending.', 'pods' ) ); |
| 220 | } |
| 221 | } else { |
| 222 | $params = [ |
| 223 | 'create_extend' => 'create', |
| 224 | 'create_pod_type' => $type, |
| 225 | 'create_storage' => $storage, |
| 226 | 'create_label_plural' => $request->get_param( 'label' ), |
| 227 | 'create_label_singular' => $request->get_param( 'label_singular' ), |
| 228 | 'create_label_menu' => $request->get_param( 'menu_name' ), |
| 229 | 'create_menu_location' => $request->get_param( 'menu_location' ), |
| 230 | ]; |
| 231 | |
| 232 | if ( 'settings' === $params['create_pod_type'] ) { |
| 233 | $params['create_label_title'] = $params['create_label_plural']; |
| 234 | } |
| 235 | |
| 236 | $name = $request->get_param( 'name' ); |
| 237 | |
| 238 | if ( 'settings' === $params['create_pod_type'] ) { |
| 239 | $params['create_setting_name'] = $name; |
| 240 | $params['create_storage'] = 'none'; |
| 241 | } else { |
| 242 | $params['create_name'] = $name; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | $api = pods_api(); |
| 247 | |
| 248 | $api->display_errors = 'wp_error'; |
| 249 | |
| 250 | $id = $api->add_pod( $params ); |
| 251 | |
| 252 | if ( is_wp_error( $id ) ) { |
| 253 | return $id; |
| 254 | } |
| 255 | |
| 256 | if ( empty( $id ) ) { |
| 257 | return new WP_Error( 'rest-object-not-added', sprintf( __( '%s not added.', 'pods' ), ucwords( $this->object ) ) ); |
| 258 | } |
| 259 | |
| 260 | return $this->get_by_args( [ |
| 261 | 'id' => $id, |
| 262 | ], 'id', null, $return_id ); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * {@inheritdoc} |
| 267 | * |
| 268 | * @since 2.8.0 |
| 269 | */ |
| 270 | public function can_create() { |
| 271 | return pods_is_admin( 'pods' ); |
| 272 | } |
| 273 | } |
| 274 |