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