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

159 lines 3.7 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 * Constructor
28 *
29 * @since 0.0.1
30 */
31 public function __construct() {
32 add_action( 'wp_ajax_export_form', [ $this, 'handle_export_form' ] );
33 add_action( 'wp_ajax_nopriv_export_form', [ $this, 'handle_export_form' ] );
34 add_action( 'rest_api_init', [ $this, 'register_custom_endpoint' ] );
35 }
36
37 /**
38 * Handle Export form
39 *
40 * @since 0.0.1
41 * @return void
42 */
43 public function handle_export_form() {
44 if ( isset( $_POST['nonce'] ) && ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'export_form_nonce' ) ) {
45 $error_message = 'Nonce verification failed.';
46
47 $error_data = [
48 'error' => $error_message,
49 ];
50 wp_send_json_error( $error_data );
51 }
52
53 if ( isset( $_POST['post_id'] ) ) {
54 $post_ids = explode( ',', sanitize_text_field( wp_unslash( $_POST['post_id'] ) ) );
55 } else {
56 $post_ids = [];
57 }
58
59 $posts = [];
60
61 foreach ( $post_ids as $post_id ) {
62 $post_id = intval( $post_id );
63 $post = get_post( $post_id );
64 $post_meta = get_post_meta( $post_id );
65 $posts[] = [
66 'post' => $post,
67 'post_meta' => $post_meta,
68 ];
69 }
70 wp_send_json( $posts );
71 }
72
73
74 /**
75 * Handle Import Form
76 *
77 * @param \WP_REST_Request $request Full details about the request.
78 *
79 * @since 0.0.1
80 * @return void
81 */
82 public function handle_import_form( $request ) {
83
84 $nonce = Helper::get_string_value( $request->get_header( 'X-WP-Nonce' ) );
85
86 if ( ! wp_verify_nonce( sanitize_text_field( $nonce ), 'wp_rest' ) ) {
87 wp_send_json_error(
88 [
89 'data' => __( 'Nonce verification failed.', 'sureforms' ),
90 'status' => false,
91 ]
92 );
93 }
94
95 // Get the raw POST data.
96 $post_data = file_get_contents( 'php://input' );
97 if ( ! $post_data ) {
98 wp_send_json_error( __( 'Failed to import form.', 'sureforms' ) );
99 }
100 $data = json_decode( $post_data, true );
101 $responses = [];
102 if ( ! is_iterable( $data ) ) {
103 wp_send_json_error( __( 'Failed to import form.', 'sureforms' ) );
104 }
105 foreach ( $data as $form_data ) {
106 $post_content = $form_data['post']['post_content'];
107 $post_title = $form_data['post']['post_title'];
108 $post_meta = $form_data['post_meta'];
109 $post_type = $form_data['post']['post_type'];
110
111 $post_content = addslashes( $post_content );
112
113 // Check if sureforms/form exists in post_content.
114 if ( 'sureforms_form' === $post_type ) {
115 $new_post = [
116 'post_title' => $post_title,
117 'post_content' => $post_content,
118 'post_status' => 'draft',
119 'post_type' => 'sureforms_form',
120 ];
121
122 $post_id = wp_insert_post( $new_post );
123 if ( ! $post_id ) {
124 http_response_code( 400 );
125 wp_send_json_error( __( 'Failed to import form.', 'sureforms' ) );
126 }
127 // Update post meta.
128 foreach ( $post_meta as $meta_key => $meta_value ) {
129 add_post_meta( $post_id, $meta_key, $meta_value[0] );
130 }
131 } else {
132 http_response_code( 400 );
133 wp_send_json_error( __( 'Failed to import form.', 'sureforms' ) );
134 }
135 }
136
137 // Return the responses.
138 wp_send_json_success( $responses );
139 }
140
141 /**
142 * Add custom API Route submit-form
143 *
144 * @return void
145 * @since 0.0.1
146 */
147 public function register_custom_endpoint() {
148 register_rest_route(
149 'sureforms/v1',
150 '/sureforms_import',
151 [
152 'methods' => WP_REST_Server::EDITABLE,
153 'callback' => [ $this, 'handle_import_form' ],
154 'permission_callback' => '__return_true',
155 ]
156 );
157 }
158 }
159