srfm-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\SRFM_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 SRFM_Export { |
| 24 | use SRFM_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 = SRFM_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 | // Check if sureforms/form exists in post_content. |
| 111 | if ( 'sureforms_form' === $post_type ) { |
| 112 | $new_post = [ |
| 113 | 'post_title' => $post_title, |
| 114 | 'post_content' => $post_content, |
| 115 | 'post_status' => 'draft', |
| 116 | 'post_type' => 'sureforms_form', |
| 117 | ]; |
| 118 | |
| 119 | $post_id = wp_insert_post( $new_post ); |
| 120 | if ( ! $post_id ) { |
| 121 | http_response_code( 400 ); |
| 122 | wp_send_json_error( __( 'Failed to import form.', 'sureforms' ) ); |
| 123 | } |
| 124 | // Update post meta. |
| 125 | foreach ( $post_meta as $meta_key => $meta_value ) { |
| 126 | add_post_meta( $post_id, $meta_key, $meta_value[0] ); |
| 127 | } |
| 128 | } else { |
| 129 | http_response_code( 400 ); |
| 130 | wp_send_json_error( __( 'Failed to import form.', 'sureforms' ) ); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Return the responses. |
| 135 | wp_send_json_success( $responses ); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Add custom API Route submit-form |
| 140 | * |
| 141 | * @return void |
| 142 | * @since 0.0.1 |
| 143 | */ |
| 144 | public function register_custom_endpoint() { |
| 145 | register_rest_route( |
| 146 | 'sureforms/v1', |
| 147 | '/sureforms_import', |
| 148 | [ |
| 149 | 'methods' => WP_REST_Server::EDITABLE, |
| 150 | 'callback' => [ $this, 'handle_import_form' ], |
| 151 | 'permission_callback' => '__return_true', |
| 152 | ] |
| 153 | ); |
| 154 | } |
| 155 | } |
| 156 |