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