PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 1.8.0
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v1.8.0
2.12.6 2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 0.0.3 All 96 releases
sureforms / inc / export.php

export.php in SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz 1.8.0, at inc/export.php

232 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Sureforms export.
4 *
5 * @package sureforms.
6 * @since 0.0.1
7 */
8
9 namespace SRFM\Inc;
10
11 use SRFM\Inc\Traits\Get_Instance;
12 use WP_REST_Server;
13
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit; // Exit if accessed directly.
16 }
17
18 /**
19 * Load Defaults Class.
20 *
21 * @since 0.0.1
22 */
23 class Export {
24 use Get_Instance;
25
26 /**
27 * Unserialized post metas.
28 *
29 * @var array<string>
30 */
31 public $unserialized_post_metas = [
32 '_srfm_conditional_logic',
33 '_srfm_email_notification',
34 '_srfm_form_confirmation',
35 '_srfm_compliance',
36 '_srfm_forms_styling',
37 '_srfm_integrations_webhooks',
38 '_srfm_instant_form_settings',
39 '_srfm_page_break_settings',
40 '_srfm_conversational_form',
41 '_srfm_premium_common',
42 '_srfm_forms_styling_starter',
43 '_srfm_user_registration_settings',
44 ];
45
46 /**
47 * Constructor
48 *
49 * @since 0.0.1
50 */
51 public function __construct() {
52 add_action( 'wp_ajax_export_form', [ $this, 'handle_export_form' ] );
53 add_action( 'rest_api_init', [ $this, 'register_custom_endpoint' ] );
54 }
55
56 /**
57 * Handle Export form
58 *
59 * @since 0.0.1
60 * @return void
61 */
62 public function handle_export_form() {
63 if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'export_form_nonce' ) ) {
64 $error_message = __( 'Nonce verification failed.', 'sureforms' );
65
66 $error_data = [
67 'error' => $error_message,
68 ];
69 wp_send_json_error( $error_data );
70 }
71
72 // check if the user has permission to export forms.
73 if ( ! Helper::current_user_can( 'manage_options' ) ) {
74 wp_send_json_error(
75 [
76 'error' => __( 'You do not have permission to export forms.', 'sureforms' ),
77 ]
78 );
79 }
80
81 if ( isset( $_POST['post_id'] ) ) {
82 $post_ids = explode( ',', sanitize_text_field( wp_unslash( $_POST['post_id'] ) ) );
83 } else {
84 $post_ids = [];
85 }
86
87 $posts = [];
88
89 foreach ( $post_ids as $post_id ) {
90 $post_id = intval( $post_id );
91 $post = get_post( $post_id );
92 $post_meta = get_post_meta( $post_id );
93 $posts[] = [
94 'post' => $post,
95 'post_meta' => $post_meta,
96 ];
97 }
98
99 // Unserialize the post metas that are serialized.
100 // This is needed because the post metas are serialized before saving.
101 foreach ( $posts as $key => $post ) {
102 $post_metas = isset( $post['post_meta'] ) && is_array( $post['post_meta'] ) ? $post['post_meta'] : [];
103 foreach ( $this->unserialized_post_metas as $meta_key ) {
104 if ( isset( $post_metas[ $meta_key ] ) && is_array( $post_metas[ $meta_key ] ) ) {
105 $post_metas[ $meta_key ] = maybe_unserialize( $post_metas[ $meta_key ][0] );
106 }
107 }
108 $posts[ $key ]['post_meta'] = $post_metas;
109 }
110
111 wp_send_json( $posts );
112 }
113
114 /**
115 * Handle Import Form
116 *
117 * @param \WP_REST_Request $request Full details about the request.
118 *
119 * @since 0.0.1
120 * @return void
121 */
122 public function handle_import_form( $request ) {
123
124 $nonce = Helper::get_string_value( $request->get_header( 'X-WP-Nonce' ) );
125
126 if ( ! wp_verify_nonce( sanitize_text_field( $nonce ), 'wp_rest' ) ) {
127 wp_send_json_error(
128 [
129 'data' => __( 'Nonce verification failed.', 'sureforms' ),
130 'status' => false,
131 ]
132 );
133 }
134
135 if ( ! current_user_can( 'manage_options' ) ) {
136 // Return error if user does not have permissions to manage options.
137 wp_send_json_error(
138 [
139 'data' => esc_html__( 'You do not have permissions to manage options.', 'sureforms' ),
140 'status' => false,
141 ]
142 );
143 }
144
145 // Get the raw POST data.
146 $post_data = file_get_contents( 'php://input' );
147 if ( ! $post_data ) {
148 wp_send_json_error( __( 'Failed to import form.', 'sureforms' ) );
149 }
150 $data = json_decode( $post_data, true );
151 if ( ! is_iterable( $data ) ) {
152 wp_send_json_error( __( 'Failed to import form.', 'sureforms' ) );
153 }
154 foreach ( $data as $form_data ) {
155
156 // sanitize the data before saving.
157 $post_content = wp_kses_post( $form_data['post']['post_content'] );
158 $post_title = sanitize_text_field( $form_data['post']['post_title'] );
159 $post_meta = $form_data['post_meta'];
160 $post_type = sanitize_text_field( $form_data['post']['post_type'] );
161
162 $post_content = addslashes( $post_content );
163
164 // Check if sureforms/form exists in post_content.
165 if ( 'sureforms_form' === $post_type ) {
166 $new_post = [
167 'post_title' => $post_title,
168 'post_status' => 'draft',
169 'post_type' => 'sureforms_form',
170 ];
171
172 $post_id = wp_insert_post( $new_post );
173
174 // Update the post content formId to the new post id.
175 $post_content = str_replace( '\"formId\":' . $form_data['post']['ID'], '\"formId\":' . $post_id, $post_content );
176
177 // update the post content.
178 wp_update_post(
179 [
180 'ID' => $post_id,
181 'post_content' => $post_content,
182 ]
183 );
184
185 if ( ! $post_id ) {
186 http_response_code( 400 );
187 wp_send_json_error( __( 'Failed to import form.', 'sureforms' ) );
188 }
189 // Update post meta.
190 foreach ( $post_meta as $meta_key => $meta_value ) {
191 // Check if the meta key is one of the unserialized post metas then add it as is.
192 if ( in_array( $meta_key, $this->unserialized_post_metas, true ) ) {
193 add_post_meta( $post_id, $meta_key, $meta_value );
194 } else {
195 if ( is_array( $meta_value ) && isset( $meta_value[0] ) ) {
196 add_post_meta( $post_id, $meta_key, $meta_value[0] );
197 } else {
198 add_post_meta( $post_id, $meta_key, $meta_value );
199 }
200 }
201 }
202 } else {
203 http_response_code( 400 );
204 wp_send_json_error( __( 'Failed to import form.', 'sureforms' ) );
205 }
206 }
207
208 // Return the responses.
209 wp_send_json_success();
210 }
211
212 /**
213 * Add custom API Route submit-form
214 *
215 * @return void
216 * @since 0.0.1
217 */
218 public function register_custom_endpoint() {
219 register_rest_route(
220 'sureforms/v1',
221 '/sureforms_import',
222 [
223 'methods' => WP_REST_Server::EDITABLE,
224 'callback' => [ $this, 'handle_import_form' ],
225 'permission_callback' => static function () {
226 return Helper::current_user_can( 'manage_options' );
227 },
228 ]
229 );
230 }
231 }
232