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