PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 1.12.3
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v1.12.3
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
229 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 * 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 * Handle Export form
71 *
72 * @since 0.0.1
73 * @return void
74 */
75 public function handle_export_form() {
76 if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'export_form_nonce' ) ) {
77 $error_message = __( 'Nonce verification failed.', 'sureforms' );
78
79 $error_data = [
80 'error' => $error_message,
81 ];
82 wp_send_json_error( $error_data );
83 }
84
85 if ( isset( $_POST['post_id'] ) ) {
86 $post_ids = explode( ',', sanitize_text_field( wp_unslash( $_POST['post_id'] ) ) );
87 } else {
88 $post_ids = [];
89 }
90
91 $posts = [];
92
93 foreach ( $post_ids as $post_id ) {
94 $post_id = intval( $post_id );
95 $post = get_post( $post_id );
96 $post_meta = get_post_meta( $post_id );
97 $posts[] = [
98 'post' => $post,
99 'post_meta' => $post_meta,
100 ];
101 }
102
103 // Unserialize the post metas that are serialized.
104 // This is needed because the post metas are serialized before saving.
105 foreach ( $posts as $key => $post ) {
106 $post_metas = isset( $post['post_meta'] ) && is_array( $post['post_meta'] ) ? $post['post_meta'] : [];
107
108 foreach ( $this->get_unserialized_post_metas() as $meta_key ) {
109 if ( isset( $post_metas[ $meta_key ] ) && is_array( $post_metas[ $meta_key ] ) ) {
110 $post_metas[ $meta_key ] = maybe_unserialize( $post_metas[ $meta_key ][0] );
111 }
112 }
113 $posts[ $key ]['post_meta'] = $post_metas;
114 }
115
116 wp_send_json( $posts );
117 }
118
119 /**
120 * Handle Import Form
121 *
122 * @param \WP_REST_Request $request Full details about the request.
123 *
124 * @since 0.0.1
125 * @return void
126 */
127 public function handle_import_form( $request ) {
128
129 $nonce = Helper::get_string_value( $request->get_header( 'X-WP-Nonce' ) );
130
131 if ( ! wp_verify_nonce( sanitize_text_field( $nonce ), 'wp_rest' ) ) {
132 wp_send_json_error(
133 [
134 'data' => __( 'Nonce verification failed.', 'sureforms' ),
135 'status' => false,
136 ]
137 );
138 }
139
140 // Get the raw POST data.
141 $post_data = file_get_contents( 'php://input' );
142 if ( ! $post_data ) {
143 wp_send_json_error( __( 'Failed to import form.', 'sureforms' ) );
144 }
145 $data = json_decode( $post_data, true );
146 if ( ! is_iterable( $data ) ) {
147 wp_send_json_error( __( 'Failed to import form.', 'sureforms' ) );
148 }
149 foreach ( $data as $form_data ) {
150
151 // sanitize the data before saving.
152 $post_content = wp_kses_post( $form_data['post']['post_content'] );
153 $post_title = sanitize_text_field( $form_data['post']['post_title'] );
154 $post_meta = $form_data['post_meta'];
155 $post_type = sanitize_text_field( $form_data['post']['post_type'] );
156
157 $post_content = addslashes( $post_content );
158
159 // Check if sureforms/form exists in post_content.
160 if ( 'sureforms_form' === $post_type ) {
161 $new_post = [
162 'post_title' => $post_title,
163 'post_status' => 'draft',
164 'post_type' => 'sureforms_form',
165 ];
166
167 $post_id = wp_insert_post( $new_post );
168
169 // Update the post content formId to the new post id.
170 $post_content = str_replace(
171 '\"formId\":' . intval( $form_data['post']['ID'] ),
172 '\"formId\":' . intval( $post_id ),
173 $post_content
174 );
175
176 // update the post content.
177 wp_update_post(
178 [
179 'ID' => $post_id,
180 'post_content' => $post_content,
181 ]
182 );
183
184 if ( ! $post_id ) {
185 http_response_code( 400 );
186 wp_send_json_error( __( 'Failed to import form.', 'sureforms' ) );
187 }
188 // Update post meta.
189 foreach ( $post_meta as $meta_key => $meta_value ) {
190 // Check if the meta key is one of the unserialized post metas then add it as is.
191 if ( in_array( $meta_key, $this->get_unserialized_post_metas(), true ) ) {
192 add_post_meta( $post_id, $meta_key, $meta_value );
193 } else {
194 if ( is_array( $meta_value ) && isset( $meta_value[0] ) ) {
195 add_post_meta( $post_id, $meta_key, $meta_value[0] );
196 } else {
197 add_post_meta( $post_id, $meta_key, $meta_value );
198 }
199 }
200 }
201 } else {
202 http_response_code( 400 );
203 wp_send_json_error( __( 'Failed to import form.', 'sureforms' ) );
204 }
205 }
206
207 // Return the responses.
208 wp_send_json_success();
209 }
210
211 /**
212 * Add custom API Route submit-form
213 *
214 * @return void
215 * @since 0.0.1
216 */
217 public function register_custom_endpoint() {
218 register_rest_route(
219 'sureforms/v1',
220 '/sureforms_import',
221 [
222 'methods' => WP_REST_Server::EDITABLE,
223 'callback' => [ $this, 'handle_import_form' ],
224 'permission_callback' => [ Helper::class, 'get_items_permissions_check' ],
225 ]
226 );
227 }
228 }
229