PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.6.14
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.6.14
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-settings-controller.php

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

130 lines 4.6 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 class Weforms_Form_Setting_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]+)/settings', [
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_settings' ],
38 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
39 'permission_callback' => [ $this, 'get_item_permissions_check' ],
40 ],
41 ] );
42
43 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<form_id>[\d]+)/settings', [
44 'args' => [
45 'form_id' => [
46 'description' => __( 'Unique identifier for the object', 'weforms' ),
47 'type' => 'integer',
48 'sanitize_callback' => 'absint',
49 'validate_callback' => [ $this, 'is_form_exists' ],
50 'required' => true,
51 ],
52 ],
53 [
54 'methods' => WP_REST_Server::READABLE,
55 'callback' => [ $this, 'get_item_settings' ],
56 'args' => [
57 'context' => $this->get_context_param( [ 'default' => 'view' ] ),
58 ],
59 'permission_callback' => [ $this, 'get_item_permissions_check' ],
60 ],
61 ] );
62 }
63
64 public function get_item_settings( $request ) {
65 $form_id = $request->get_param( 'form_id' );
66 $form = weforms()->form->get( $form_id );
67 $data = $form->get_settings();
68
69 $response = $this->prepare_response_for_collection( $data, $request );
70 $response = rest_ensure_response( $response );
71 $response->header( 'X-WP-Total', (int) count( $data ) );
72
73 return $response;
74 }
75
76 public function update_item_settings( $request ) {
77 $form_id = $request->get_param( 'form_id' );
78 $settings = $request['settings'];
79 $form = weforms()->form->get( $form_id );
80 $form_settings = $form->get_settings();
81
82 if ( isset( $settings ) && !empty( $settings ) ) {
83 $new_form_settings = array_merge( $settings, array_diff_key( $form->get_settings(), $settings ) );
84 update_post_meta( $form_id, 'wpuf_form_settings', $new_form_settings );
85 }
86
87 $response_data = [
88 'id' => $form->data->ID,
89 'settings' => $form->get_settings(),
90 ];
91
92 $response = rest_ensure_response( $response_data );
93 $response->set_status( 201 );
94
95 return $response;
96 }
97
98 /**
99 * Get the Form's schema, conforming to JSON Schema
100 *
101 * @return array
102 */
103 public function get_item_schema() {
104 $schema = [
105 '$schema' => 'http://json-schema.org/draft-04/schema#',
106 'title' => 'forms',
107 'type' => 'object',
108 'properties' => [
109 'form_id' => [
110 'description' => __( 'Unique identifier for the object', 'weforms' ),
111 'type' => 'integer',
112 'sanitize_callback' => 'absint',
113 'validate_callback' => [ $this, 'is_form_exists' ],
114 'context' => [ 'embed', 'view', 'edit' ],
115 'required' => true,
116 'readonly' => true,
117 ],
118 'settings' => [
119 'description' => __( '', 'weforms' ),
120 'type' => 'object',
121 'context' => [ 'edit', 'view'],
122 'required' => false,
123 ],
124 ],
125 ];
126
127 return $schema;
128 }
129 }
130