PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.4.1
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.4.1
1.6.7 1.6.8 1.6.9 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.18 1.6.19 1.6.2 1.6.20 1.6.21 1.6.22 1.6.23 1.6.24 1.6.25 1.6.26 1.6.27 1.6.28 1.6.3 1.6.4 1.6.5 1.6.6 All 74 releases
weforms / includes / api / class-weforms-form-fields-controller.php

class-weforms-form-fields-controller.php in weForms – Easy Drag & Drop Contact Form Builder For WordPress 1.4.1, at includes/api/class-weforms-form-fields-controller.php

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