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

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

155 lines 5.4 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_Integration_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]+)/integrations',
27 array(
28 'args' => array(
29 'form_id' => array(
30 'description' => __( 'Unique identifier for the object', 'weforms' ),
31 'type' => 'integer',
32 'sanitize_callback' => 'absint',
33 'validate_callback' => array( $this, 'is_form_exists' ),
34 'required' => true,
35 ),
36 ),
37 array(
38 'methods' => WP_REST_Server::READABLE,
39 'callback' => array( $this, 'get_item_integrations' ),
40 'args' => array(
41 'context' => $this->get_context_param( [ 'default' => 'view' ] )
42 ),
43 'permission_callback' => array( $this, 'get_item_permissions_check' ),
44 ),
45 array(
46 'methods' => WP_REST_Server::EDITABLE,
47 'callback' => array( $this, 'update_item_integrations' ),
48 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
49 'permission_callback' => array( $this, 'get_item_permissions_check' ),
50 ),
51 )
52 );
53 }
54
55 /**
56 * get item integrations
57 *
58 * @since 1.4.2
59 *
60 * @param WP_REST_Request $request
61 *
62 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
63 */
64 public function get_item_integrations( $request ) {
65 $form_id = $request->get_param( 'form_id' );
66 $form = weforms()->form->get( $form_id );
67 $data = $form->get_integrations();
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['integrations'] ) );
72
73 return $response;
74 }
75
76 /**
77 * [update_item_integrations description]
78 * @param [type] $request [description]
79 * @return [type] [description]
80 */
81 public function update_item_integrations( $request ) {
82 $wpuf_form_id = $request->get_param('form_id');
83 $integrations = $request->get_param('integrations');
84
85 $integration_list = weforms()->integrations->get_integration_js_settings();
86
87 $form = weforms()->form->get( $wpuf_form_id );
88 $form_integration = $form->get_integrations();
89 $integrations = array_intersect_key( $integrations, $integration_list );
90
91 if ( !class_exists( 'WeForms_Pro' ) ) {
92 $integrations = array_udiff_assoc( $integrations, $integration_list,
93 function( $item, $item_list ) {
94 if( isset( $item_list['pro'] ) && $item_list['pro'] == true ) {
95 return 0;
96 }
97 return $item;
98 }
99 );
100 }
101
102 $data = array(
103 'form_id' => $wpuf_form_id,
104 'integrations' => $integrations
105 );
106
107 $new_form_integrations = array_merge( $data['integrations'], array_diff_key( $form->get_integrations(), $data['integrations'] ) );
108
109 update_post_meta( $data['form_id'], 'integrations', $new_form_integrations );
110
111 $response_data = array(
112 'id' => $form->data->ID,
113 'integrations' => $form->get_integrations()
114 );
115
116 $response = rest_ensure_response( $response_data );
117 $response->set_status( 201 );
118 $response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $form->id ) ) );
119
120 return $response;
121 }
122
123 /**
124 * Get the Form's schema, conforming to JSON Schema
125 *
126 * @return array
127 */
128 public function get_item_schema() {
129 $schema = array(
130 '$schema' => 'http://json-schema.org/draft-04/schema#',
131 'title' => 'forms',
132 'type' => 'object',
133 'properties' => array(
134 'form_id' => array(
135 'description' => __( 'Unique identifier for the object', 'weforms' ),
136 'type' => 'integer',
137 'sanitize_callback' => 'absint',
138 'validate_callback' => array( $this, 'is_form_exists' ),
139 'context' => array( 'embed', 'view', 'edit' ),
140 'required' => true,
141 'readonly' => true,
142 ),
143 "integrations" => array(
144 'description' => __( '', 'weforms' ),
145 'type' => 'object',
146 'context' => [ 'edit' ,'view'],
147 'required' => false,
148 ),
149 ),
150 );
151
152 return $schema;
153 }
154 }
155