PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 1.13.0
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v1.13.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
267 lines 7.2 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 * Get unserialized post meta keys.
58 *
59 * Retrieves the list of post meta keys that need to be unserialized during export.
60 * Allows filtering of meta keys via 'srfm_export_and_import_post_meta_keys' filter.
61 *
62 * @since 1.9.0
63 * @return array<string> Array of post meta keys to unserialize.
64 */
65 public function get_unserialized_post_metas() {
66 return Helper::apply_filters_as_array( 'srfm_export_and_import_post_meta_keys', $this->unserialized_post_metas );
67 }
68
69 /**
70 * Get forms with meta by post IDs.
71 * Uses:
72 * - On websitedemos.net, for exporting the Spectra Block Patterns & Pages with SureForms form.
73 *
74 * @since 1.13.0
75 * @param array<int, string> $post_ids Array of post IDs to retrieve forms for.
76 * @return array Array of forms with their post data and meta data.
77 */
78 public function get_forms_with_meta( $post_ids = [] ) {
79 $posts = [];
80
81 foreach ( $post_ids as $post_id ) {
82 $post_id = intval( $post_id );
83 $post = get_post( $post_id );
84 $post_meta = get_post_meta( $post_id );
85 $posts[] = [
86 'post' => $post,
87 'post_meta' => $post_meta,
88 ];
89 }
90
91 // Unserialize the post metas that are serialized.
92 // This is needed because the post metas are serialized before saving.
93 foreach ( $posts as $key => $post ) {
94 $post_metas = isset( $post['post_meta'] ) && is_array( $post['post_meta'] ) ? $post['post_meta'] : [];
95
96 foreach ( $this->get_unserialized_post_metas() as $meta_key ) {
97 if ( isset( $post_metas[ $meta_key ] ) && is_array( $post_metas[ $meta_key ] ) ) {
98 $post_metas[ $meta_key ] = maybe_unserialize( $post_metas[ $meta_key ][0] );
99 }
100 }
101 $posts[ $key ]['post_meta'] = $post_metas;
102 }
103
104 return $posts;
105 }
106
107 /**
108 * Handle Export form
109 *
110 * @since 0.0.1
111 * @return void
112 */
113 public function handle_export_form() {
114 if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'export_form_nonce' ) ) {
115 $error_message = __( 'Nonce verification failed.', 'sureforms' );
116
117 $error_data = [
118 'error' => $error_message,
119 ];
120 wp_send_json_error( $error_data );
121 }
122
123 if ( isset( $_POST['post_id'] ) ) {
124 $post_ids = explode( ',', sanitize_text_field( wp_unslash( $_POST['post_id'] ) ) );
125 } else {
126 $post_ids = [];
127 }
128
129 $posts = $this->get_forms_with_meta( $post_ids );
130 wp_send_json( $posts );
131 }
132
133 /**
134 * Import Forms with Meta
135 * Uses:
136 * - In Design Library for importing the Spectra Block Patterns and Pages with SureForms form.
137 *
138 * @param array<array<array<string>>> $data Form data to import.
139 * @param string $default_status Default post status for imported forms. Default is 'draft'.
140 *
141 * @since 1.13.0
142 * @return array<int, int>|\WP_Error Returns mapping array on success, WP_Error on failure.
143 */
144 public function import_forms_with_meta( $data, $default_status = 'draft' ) {
145 $forms_mapping = [];
146 foreach ( $data as $form_data ) {
147 // sanitize the data before saving.
148 $old_id = intval( $form_data['post']['ID'] );
149 $post_content = wp_kses_post( $form_data['post']['post_content'] );
150 $post_title = sanitize_text_field( $form_data['post']['post_title'] );
151 $post_meta = $form_data['post_meta'];
152 $post_type = sanitize_text_field( $form_data['post']['post_type'] );
153
154 $post_content = addslashes( $post_content );
155
156 // Check if sureforms/form exists in post_content.
157 if ( 'sureforms_form' === $post_type ) {
158 $new_post = [
159 'post_title' => $post_title,
160 'post_status' => $default_status,
161 'post_type' => 'sureforms_form',
162 ];
163
164 $post_id = wp_insert_post( $new_post );
165
166 // Update the post content formId to the new post id.
167 $post_content = str_replace(
168 '\"formId\":' . intval( $form_data['post']['ID'] ),
169 '\"formId\":' . intval( $post_id ),
170 $post_content
171 );
172
173 // update the post content.
174 wp_update_post(
175 [
176 'ID' => $post_id,
177 'post_content' => $post_content,
178 ]
179 );
180
181 if ( ! $post_id ) {
182 return new \WP_Error( 'import_forms_failed', __( 'Failed to import form.', 'sureforms' ) );
183 }
184
185 $forms_mapping[ $old_id ] = $post_id;
186
187 // Update post meta.
188 foreach ( $post_meta as $meta_key => $meta_value ) {
189 // Check if the meta key is one of the unserialized post metas then add it as is.
190 if ( in_array( $meta_key, $this->get_unserialized_post_metas(), true ) ) {
191 add_post_meta( $post_id, $meta_key, $meta_value );
192 } else {
193 if ( is_array( $meta_value ) && isset( $meta_value[0] ) ) {
194 add_post_meta( $post_id, $meta_key, $meta_value[0] );
195 } else {
196 add_post_meta( $post_id, $meta_key, $meta_value );
197 }
198 }
199 }
200 } else {
201 return new \WP_Error( 'import_forms_invalid_post_type', __( 'Failed to import form.', 'sureforms' ) );
202 }
203 }
204
205 return $forms_mapping;
206 }
207
208 /**
209 * Handle Import Form
210 *
211 * @param \WP_REST_Request $request Full details about the request.
212 *
213 * @since 0.0.1
214 * @return void
215 */
216 public function handle_import_form( $request ) {
217
218 $nonce = Helper::get_string_value( $request->get_header( 'X-WP-Nonce' ) );
219
220 if ( ! wp_verify_nonce( sanitize_text_field( $nonce ), 'wp_rest' ) ) {
221 wp_send_json_error(
222 [
223 'data' => __( 'Nonce verification failed.', 'sureforms' ),
224 'status' => false,
225 ]
226 );
227 }
228
229 // Get the raw POST data.
230 $post_data = file_get_contents( 'php://input' );
231 if ( ! $post_data ) {
232 wp_send_json_error( __( 'Failed to import form.', 'sureforms' ) );
233 }
234 $data = json_decode( $post_data, true );
235 if ( ! is_iterable( $data ) ) {
236 wp_send_json_error( __( 'Failed to import form.', 'sureforms' ) );
237 }
238
239 $result = $this->import_forms_with_meta( $data );
240 if ( is_wp_error( $result ) ) {
241 http_response_code( 400 );
242 wp_send_json_error( $result->get_error_message() );
243 }
244
245 // Return the responses.
246 wp_send_json_success();
247 }
248
249 /**
250 * Add custom API Route submit-form
251 *
252 * @return void
253 * @since 0.0.1
254 */
255 public function register_custom_endpoint() {
256 register_rest_route(
257 'sureforms/v1',
258 '/sureforms_import',
259 [
260 'methods' => WP_REST_Server::EDITABLE,
261 'callback' => [ $this, 'handle_import_form' ],
262 'permission_callback' => [ Helper::class, 'get_items_permissions_check' ],
263 ]
264 );
265 }
266 }
267