PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 1.0.2
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v1.0.2
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.0.2, at inc/export.php

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