← All changes
|
includes/api/class-weforms-form-fields-controller.php
+215
-215
1.6.3
→
1.4.9
View file →
| @@ -1,215 +1,215 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -/** | |
| 4 | - * Settings manager class | |
| 5 | - * | |
| 6 | - * @since 1.4.2 | |
| 7 | - */ | |
| 8 | -class Weforms_Form_Fields_Controller extends Weforms_REST_Controller { | |
| 9 | - | |
| 10 | - /** | |
| 11 | - * Endpoint namespace | |
| 12 | - * | |
| 13 | - * @var string | |
| 14 | - */ | |
| 15 | - protected $namespace = 'weforms/v1'; | |
| 16 | - | |
| 17 | - /** | |
| 18 | - * Route name | |
| 19 | - * | |
| 20 | - * @var string | |
| 21 | - */ | |
| 22 | - protected $rest_base = 'forms'; | |
| 23 | - | |
| 24 | - public function register_routes() { | |
| 25 | - register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<form_id>[\d]+)/fields', [ | |
| 26 | - 'args' => [ | |
| 27 | - 'form_id' => [ | |
| 28 | - 'description' => __( 'Unique identifier for the object', 'weforms' ), | |
| 29 | - 'type' => 'integer', | |
| 30 | - 'sanitize_callback' => 'absint', | |
| 31 | - 'validate_callback' => [ $this, 'is_form_exists' ], | |
| 32 | - 'required' => true, | |
| 33 | - ], | |
| 34 | - ], | |
| 35 | - [ | |
| 36 | - 'methods' => WP_REST_Server::EDITABLE, | |
| 37 | - 'callback' => [ $this, 'update_item_fields' ], | |
| 38 | - 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), | |
| 39 | - 'permission_callback' => [ $this, 'get_item_permissions_check' ], | |
| 40 | - ], | |
| 41 | - [ | |
| 42 | - 'methods' => WP_REST_Server::READABLE, | |
| 43 | - 'callback' => [ $this, 'get_item_fields' ], | |
| 44 | - 'args' => [ | |
| 45 | - 'context' => $this->get_context_param( [ 'default' => 'view' ] ), | |
| 46 | - ], | |
| 47 | - 'permission_callback' => [ $this, 'get_item_permissions_check' ], | |
| 48 | - ], | |
| 49 | - | |
| 50 | - [ | |
| 51 | - 'methods' => WP_REST_Server::DELETABLE, | |
| 52 | - 'callback' => [ $this, 'delete_item_fields' ], | |
| 53 | - 'args' => [ | |
| 54 | - 'field_id' => [ | |
| 55 | - 'description' => __( '', 'weforms' ), | |
| 56 | - 'type' => 'array', | |
| 57 | - 'validate_callback' => [ $this, 'is_field_exists' ], | |
| 58 | - 'required' => true, | |
| 59 | - ], | |
| 60 | - ], | |
| 61 | - 'permission_callback' => [ $this, 'get_item_permissions_check' ], | |
| 62 | - ], | |
| 63 | - ] | |
| 64 | - ); | |
| 65 | - } | |
| 66 | - | |
| 67 | - /** | |
| 68 | - * [update_item_settingss description] | |
| 69 | - * | |
| 70 | - * @param [type] $request [description] | |
| 71 | - * | |
| 72 | - * @return [type] [description] | |
| 73 | - */ | |
| 74 | - public function update_item_fields( $request ) { | |
| 75 | - $form_id = $request->get_param( 'form_id' ); | |
| 76 | - $form_fields = $request->get_param( 'fields' ); | |
| 77 | - | |
| 78 | - $data = [ | |
| 79 | - 'form_id' => $form_id, | |
| 80 | - 'form_fields' => $form_fields, | |
| 81 | - ]; | |
| 82 | - | |
| 83 | - $existing_wpuf_input_ids = get_children( [ | |
| 84 | - 'post_parent' => $data['form_id'], | |
| 85 | - 'post_status' => 'publish', | |
| 86 | - 'post_type' => 'wpuf_input', | |
| 87 | - 'numberposts' => '-1', | |
| 88 | - 'orderby' => 'menu_order', | |
| 89 | - 'order' => 'ASC', | |
| 90 | - 'fields' => 'ids', | |
| 91 | - ] ); | |
| 92 | - | |
| 93 | - $new_wpuf_input_ids = []; | |
| 94 | - | |
| 95 | - if ( !empty( $data['form_fields'] ) ) { | |
| 96 | - foreach ( $data['form_fields'] as $order => $field ) { | |
| 97 | - if ( !empty( $field['is_new'] ) ) { | |
| 98 | - unset( $field['is_new'] ); | |
| 99 | - unset( $field['id'] ); | |
| 100 | - | |
| 101 | - $field_id = 0; | |
| 102 | - } else { | |
| 103 | - $field_id = $field['id']; | |
| 104 | - } | |
| 105 | - | |
| 106 | - $field_id = weforms_insert_form_field( $data['form_id'], $field, $field_id, $order ); | |
| 107 | - | |
| 108 | - $new_wpuf_input_ids[] = $field_id; | |
| 109 | - | |
| 110 | - $field['id'] = $field_id; | |
| 111 | - | |
| 112 | - $saved_wpuf_inputs[] = $field; | |
| 113 | - } | |
| 114 | - } | |
| 115 | - | |
| 116 | - $form = weforms()->form->get( $form_id ); | |
| 117 | - | |
| 118 | - $response_data = [ | |
| 119 | - 'id' => $form->data->ID, | |
| 120 | - 'fields' => $form->get_fields(), | |
| 121 | - ]; | |
| 122 | - | |
| 123 | - $request->set_param( 'context', 'edit' ); | |
| 124 | - $response = $this->prepare_item_for_response( $response_data, $request ); | |
| 125 | - $response = rest_ensure_response( $response ); | |
| 126 | - $response->set_status( 201 ); | |
| 127 | - $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $form->id ) ) ); | |
| 128 | - | |
| 129 | - return $response; | |
| 130 | - } | |
| 131 | - | |
| 132 | - public function get_item_fields( $request ) { | |
| 133 | - $form_id = $request->get_param( 'form_id' ); | |
| 134 | - $form = weforms()->form->get( $form_id ); | |
| 135 | - $data = $form->get_fields(); | |
| 136 | - | |
| 137 | - $response = $this->prepare_response_for_collection( $data, $request ); | |
| 138 | - $response = rest_ensure_response( $response ); | |
| 139 | - $response->header( 'X-WP-Total', (int) count( $data ) ); | |
| 140 | - | |
| 141 | - return $response; | |
| 142 | - } | |
| 143 | - | |
| 144 | - public function delete_item_fields( $request ) { | |
| 145 | - $form_id = $request->get_param( 'form_id' ); | |
| 146 | - $field_ids = $request->get_param( 'field_id' ); | |
| 147 | - | |
| 148 | - if ( empty( $field_ids ) ) { | |
| 149 | - return new WP_Error( 'error', __( 'Fields id not provided', 'weforms' ), [ 'status' => 404 ] ); | |
| 150 | - } | |
| 151 | - | |
| 152 | - $fields = get_children( [ | |
| 153 | - 'post_parent' => $form_id, | |
| 154 | - 'post_status' => 'publish', | |
| 155 | - 'post_type' => 'wpuf_input', | |
| 156 | - 'numberposts' => '-1', | |
| 157 | - 'orderby' => 'menu_order', | |
| 158 | - 'order' => 'ASC', | |
| 159 | - 'fields' => 'ids', | |
| 160 | - ] ); | |
| 161 | - | |
| 162 | - $deleted_fields = []; | |
| 163 | - | |
| 164 | - foreach ( $field_ids as $field_id ) { | |
| 165 | - if ( in_array( $field_id, $fields ) ) { | |
| 166 | - $deleted_fields[] = wp_delete_post( $field_id, true ); | |
| 167 | - } | |
| 168 | - } | |
| 169 | - | |
| 170 | - if ( empty( $deleted_fields ) ) { | |
| 171 | - return new WP_Error( 'error', __( 'Fields not exist or deleted before.', 'weforms' ), [ 'status' => 404 ] ); | |
| 172 | - } | |
| 173 | - | |
| 174 | - $data = [ | |
| 175 | - 'message' => __( 'Fields deleted successfully ', 'weforms' ), | |
| 176 | - ]; | |
| 177 | - | |
| 178 | - $response = $this->prepare_response_for_collection( $data, $request ); | |
| 179 | - $response = rest_ensure_response( $response ); | |
| 180 | - | |
| 181 | - return $response; | |
| 182 | - } | |
| 183 | - | |
| 184 | - /** | |
| 185 | - * Get the Form's schema, conforming to JSON Schema | |
| 186 | - * | |
| 187 | - * @return array | |
| 188 | - */ | |
| 189 | - public function get_item_schema() { | |
| 190 | - $schema = [ | |
| 191 | - '$schema' => 'http://json-schema.org/draft-04/schema#', | |
| 192 | - 'title' => 'forms', | |
| 193 | - 'type' => 'object', | |
| 194 | - 'properties' => [ | |
| 195 | - 'form_id' => [ | |
| 196 | - 'description' => __( 'Unique identifier for the object', 'weforms' ), | |
| 197 | - 'type' => 'integer', | |
| 198 | - 'sanitize_callback' => 'absint', | |
| 199 | - 'validate_callback' => [ $this, 'is_form_exists' ], | |
| 200 | - 'context' => [ 'embed', 'view', 'edit' ], | |
| 201 | - 'required' => true, | |
| 202 | - 'readonly' => true, | |
| 203 | - ], | |
| 204 | - 'fields' => [ | |
| 205 | - 'description' => __( '', 'weforms' ), | |
| 206 | - 'type' => 'object', | |
| 207 | - 'context' => [ 'edit', 'view'], | |
| 208 | - 'required' => false, | |
| 209 | - ], | |
| 210 | - ], | |
| 211 | - ]; | |
| 212 | - | |
| 213 | - return $schema; | |
| 214 | - } | |
| 215 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * Settings manager class | |
| 5 | + * | |
| 6 | + * @since 1.4.2 | |
| 7 | + */ | |
| 8 | +class Weforms_Form_Fields_Controller extends Weforms_REST_Controller { | |
| 9 | + | |
| 10 | + /** | |
| 11 | + * Endpoint namespace | |
| 12 | + * | |
| 13 | + * @var string | |
| 14 | + */ | |
| 15 | + protected $namespace = 'weforms/v1'; | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * Route name | |
| 19 | + * | |
| 20 | + * @var string | |
| 21 | + */ | |
| 22 | + protected $rest_base = 'forms'; | |
| 23 | + | |
| 24 | + public function register_routes() { | |
| 25 | + register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<form_id>[\d]+)/fields', [ | |
| 26 | + 'args' => [ | |
| 27 | + 'form_id' => [ | |
| 28 | + 'description' => __( 'Unique identifier for the object', 'weforms' ), | |
| 29 | + 'type' => 'integer', | |
| 30 | + 'sanitize_callback' => 'absint', | |
| 31 | + 'validate_callback' => [ $this, 'is_form_exists' ], | |
| 32 | + 'required' => true, | |
| 33 | + ], | |
| 34 | + ], | |
| 35 | + [ | |
| 36 | + 'methods' => WP_REST_Server::EDITABLE, | |
| 37 | + 'callback' => [ $this, 'update_item_fields' ], | |
| 38 | + 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), | |
| 39 | + 'permission_callback' => [ $this, 'get_item_permissions_check' ], | |
| 40 | + ], | |
| 41 | + [ | |
| 42 | + 'methods' => WP_REST_Server::READABLE, | |
| 43 | + 'callback' => [ $this, 'get_item_fields' ], | |
| 44 | + 'args' => [ | |
| 45 | + 'context' => $this->get_context_param( [ 'default' => 'view' ] ), | |
| 46 | + ], | |
| 47 | + 'permission_callback' => [ $this, 'get_item_permissions_check' ], | |
| 48 | + ], | |
| 49 | + | |
| 50 | + [ | |
| 51 | + 'methods' => WP_REST_Server::DELETABLE, | |
| 52 | + 'callback' => [ $this, 'delete_item_fields' ], | |
| 53 | + 'args' => [ | |
| 54 | + 'field_id' => [ | |
| 55 | + 'description' => __( '', 'weforms' ), | |
| 56 | + 'type' => 'array', | |
| 57 | + 'validate_callback' => [ $this, 'is_field_exists' ], | |
| 58 | + 'required' => true, | |
| 59 | + ], | |
| 60 | + ], | |
| 61 | + 'permission_callback' => [ $this, 'get_item_permissions_check' ], | |
| 62 | + ], | |
| 63 | + ] | |
| 64 | + ); | |
| 65 | + } | |
| 66 | + | |
| 67 | + /** | |
| 68 | + * [update_item_settingss description] | |
| 69 | + * | |
| 70 | + * @param [type] $request [description] | |
| 71 | + * | |
| 72 | + * @return [type] [description] | |
| 73 | + */ | |
| 74 | + public function update_item_fields( $request ) { | |
| 75 | + $form_id = $request->get_param( 'form_id' ); | |
| 76 | + $form_fields = $request->get_param( 'fields' ); | |
| 77 | + | |
| 78 | + $data = [ | |
| 79 | + 'form_id' => $form_id, | |
| 80 | + 'form_fields' => $form_fields, | |
| 81 | + ]; | |
| 82 | + | |
| 83 | + $existing_wpuf_input_ids = get_children( [ | |
| 84 | + 'post_parent' => $data['form_id'], | |
| 85 | + 'post_status' => 'publish', | |
| 86 | + 'post_type' => 'wpuf_input', | |
| 87 | + 'numberposts' => '-1', | |
| 88 | + 'orderby' => 'menu_order', | |
| 89 | + 'order' => 'ASC', | |
| 90 | + 'fields' => 'ids', | |
| 91 | + ] ); | |
| 92 | + | |
| 93 | + $new_wpuf_input_ids = []; | |
| 94 | + | |
| 95 | + if ( !empty( $data['form_fields'] ) ) { | |
| 96 | + foreach ( $data['form_fields'] as $order => $field ) { | |
| 97 | + if ( !empty( $field['is_new'] ) ) { | |
| 98 | + unset( $field['is_new'] ); | |
| 99 | + unset( $field['id'] ); | |
| 100 | + | |
| 101 | + $field_id = 0; | |
| 102 | + } else { | |
| 103 | + $field_id = $field['id']; | |
| 104 | + } | |
| 105 | + | |
| 106 | + $field_id = weforms_insert_form_field( $data['form_id'], $field, $field_id, $order ); | |
| 107 | + | |
| 108 | + $new_wpuf_input_ids[] = $field_id; | |
| 109 | + | |
| 110 | + $field['id'] = $field_id; | |
| 111 | + | |
| 112 | + $saved_wpuf_inputs[] = $field; | |
| 113 | + } | |
| 114 | + } | |
| 115 | + | |
| 116 | + $form = weforms()->form->get( $form_id ); | |
| 117 | + | |
| 118 | + $response_data = [ | |
| 119 | + 'id' => $form->data->ID, | |
| 120 | + 'fields' => $form->get_fields(), | |
| 121 | + ]; | |
| 122 | + | |
| 123 | + $request->set_param( 'context', 'edit' ); | |
| 124 | + $response = $this->prepare_item_for_response( $response_data, $request ); | |
| 125 | + $response = rest_ensure_response( $response ); | |
| 126 | + $response->set_status( 201 ); | |
| 127 | + $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $form->id ) ) ); | |
| 128 | + | |
| 129 | + return $response; | |
| 130 | + } | |
| 131 | + | |
| 132 | + public function get_item_fields( $request ) { | |
| 133 | + $form_id = $request->get_param( 'form_id' ); | |
| 134 | + $form = weforms()->form->get( $form_id ); | |
| 135 | + $data = $form->get_fields(); | |
| 136 | + | |
| 137 | + $response = $this->prepare_response_for_collection( $data, $request ); | |
| 138 | + $response = rest_ensure_response( $response ); | |
| 139 | + $response->header( 'X-WP-Total', (int) count( $data ) ); | |
| 140 | + | |
| 141 | + return $response; | |
| 142 | + } | |
| 143 | + | |
| 144 | + public function delete_item_fields( $request ) { | |
| 145 | + $form_id = $request->get_param( 'form_id' ); | |
| 146 | + $field_ids = $request->get_param( 'field_id' ); | |
| 147 | + | |
| 148 | + if ( empty( $field_ids ) ) { | |
| 149 | + return new WP_Error( 'error', __( 'Fields id not provided', 'weforms' ), [ 'status' => 404 ] ); | |
| 150 | + } | |
| 151 | + | |
| 152 | + $fields = get_children( [ | |
| 153 | + 'post_parent' => $form_id, | |
| 154 | + 'post_status' => 'publish', | |
| 155 | + 'post_type' => 'wpuf_input', | |
| 156 | + 'numberposts' => '-1', | |
| 157 | + 'orderby' => 'menu_order', | |
| 158 | + 'order' => 'ASC', | |
| 159 | + 'fields' => 'ids', | |
| 160 | + ] ); | |
| 161 | + | |
| 162 | + $deleted_fields = []; | |
| 163 | + | |
| 164 | + foreach ( $field_ids as $field_id ) { | |
| 165 | + if ( in_array( $field_id, $fields ) ) { | |
| 166 | + $deleted_fields[] = wp_delete_post( $field_id, true ); | |
| 167 | + } | |
| 168 | + } | |
| 169 | + | |
| 170 | + if ( empty( $deleted_fields ) ) { | |
| 171 | + return new WP_Error( 'error', __( 'Fields not exist or deleted before.', 'weforms' ), [ 'status' => 404 ] ); | |
| 172 | + } | |
| 173 | + | |
| 174 | + $data = [ | |
| 175 | + 'message' => __( 'Fields deleted successfully ', 'weforms' ), | |
| 176 | + ]; | |
| 177 | + | |
| 178 | + $response = $this->prepare_response_for_collection( $data, $request ); | |
| 179 | + $response = rest_ensure_response( $response ); | |
| 180 | + | |
| 181 | + return $response; | |
| 182 | + } | |
| 183 | + | |
| 184 | + /** | |
| 185 | + * Get the Form's schema, conforming to JSON Schema | |
| 186 | + * | |
| 187 | + * @return array | |
| 188 | + */ | |
| 189 | + public function get_item_schema() { | |
| 190 | + $schema = [ | |
| 191 | + '$schema' => 'http://json-schema.org/draft-04/schema#', | |
| 192 | + 'title' => 'forms', | |
| 193 | + 'type' => 'object', | |
| 194 | + 'properties' => [ | |
| 195 | + 'form_id' => [ | |
| 196 | + 'description' => __( 'Unique identifier for the object', 'weforms' ), | |
| 197 | + 'type' => 'integer', | |
| 198 | + 'sanitize_callback' => 'absint', | |
| 199 | + 'validate_callback' => [ $this, 'is_form_exists' ], | |
| 200 | + 'context' => [ 'embed', 'view', 'edit' ], | |
| 201 | + 'required' => true, | |
| 202 | + 'readonly' => true, | |
| 203 | + ], | |
| 204 | + 'fields' => [ | |
| 205 | + 'description' => __( '', 'weforms' ), | |
| 206 | + 'type' => 'object', | |
| 207 | + 'context' => [ 'edit', 'view'], | |
| 208 | + 'required' => false, | |
| 209 | + ], | |
| 210 | + ], | |
| 211 | + ]; | |
| 212 | + | |
| 213 | + return $schema; | |
| 214 | + } | |
| 215 | +} | |