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