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-settings-controller.php

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

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