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