| 1 |
<?php |
| 2 |
/** |
| 3 |
* Create new Form with Template and return the form ID. |
| 4 |
* |
| 5 |
* @package sureforms. |
| 6 |
* @since 0.0.1 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SRFM\Inc; |
| 10 |
|
| 11 |
use WP_REST_Response; |
| 12 |
use WP_REST_Request; |
| 13 |
use WP_Error; |
| 14 |
use WP_Post_Type; |
| 15 |
use SRFM\Inc\Traits\Get_Instance; |
| 16 |
use SRFM\Inc\Helper; |
| 17 |
|
| 18 |
if ( ! defined( 'ABSPATH' ) ) { |
| 19 |
exit; // Exit if accessed directly. |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Create New Form. |
| 24 |
* |
| 25 |
* @since 0.0.1 |
| 26 |
*/ |
| 27 |
class Create_New_Form { |
| 28 |
use Get_Instance; |
| 29 |
|
| 30 |
/** |
| 31 |
* Constructor |
| 32 |
* |
| 33 |
* @since 0.0.1 |
| 34 |
*/ |
| 35 |
public function __construct() { |
| 36 |
add_action( 'rest_api_init', [ $this, 'register_custom_endpoint' ] ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Add custom API Route create-new-form. |
| 41 |
* |
| 42 |
* @return void |
| 43 |
* @since 0.0.1 |
| 44 |
*/ |
| 45 |
public function register_custom_endpoint() { |
| 46 |
register_rest_route( |
| 47 |
'sureforms/v1', |
| 48 |
'/create-new-form', |
| 49 |
[ |
| 50 |
'methods' => 'POST', |
| 51 |
'callback' => [ $this, 'create_form' ], |
| 52 |
'permission_callback' => [ $this, 'get_items_permissions_check' ], |
| 53 |
] |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Checks whether a given request has permission to create new forms. |
| 59 |
* |
| 60 |
* @param WP_REST_Request $request Full details about the request. |
| 61 |
* @return true|WP_Error True if the request has read access, WP_Error object otherwise. |
| 62 |
* @since 0.0.1 |
| 63 |
*/ |
| 64 |
public function get_items_permissions_check( $request ) { |
| 65 |
if ( current_user_can( 'edit_posts' ) ) { |
| 66 |
return true; |
| 67 |
} |
| 68 |
foreach ( get_post_types( [ 'show_in_rest' => true ], 'objects' ) as $post_type ) { |
| 69 |
/** |
| 70 |
* The post type. |
| 71 |
* |
| 72 |
* @var WP_Post_Type $post_type |
| 73 |
*/ |
| 74 |
if ( current_user_can( $post_type->cap->edit_posts ) ) { |
| 75 |
return true; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
return new \WP_Error( |
| 80 |
'rest_cannot_view', |
| 81 |
__( 'Sorry, you are not allowed to create forms.', 'sureforms' ), |
| 82 |
[ 'status' => \rest_authorization_required_code() ] |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Get default post metas for form when creating using template. |
| 88 |
* |
| 89 |
* @return array<string, array<int, int|string>> Default meta keys. |
| 90 |
* @since 0.0.2 |
| 91 |
*/ |
| 92 |
public static function get_default_meta_keys() { |
| 93 |
return [ |
| 94 |
'_srfm_submit_button_text' => [ 'Submit' ], |
| 95 |
'_srfm_use_label_as_placeholder' => [ '' ], |
| 96 |
'_srfm_single_page_form_title' => [ 1 ], |
| 97 |
'_srfm_instant_form' => [ '' ], |
| 98 |
'_srfm_form_container_width' => [ 650 ], |
| 99 |
'_srfm_bg_type' => [ 'image' ], |
| 100 |
'_srfm_bg_image' => [ '' ], |
| 101 |
'_srfm_cover_image' => [ '' ], |
| 102 |
'_srfm_bg_color' => [ '#ffffff' ], |
| 103 |
'_srfm_submit_width_backend' => [ 'max-content' ], |
| 104 |
'_srfm_submit_alignment' => [ 'left' ], |
| 105 |
'_srfm_submit_alignment_backend' => [ '100%' ], |
| 106 |
'_srfm_submit_width' => [ '' ], |
| 107 |
'_srfm_inherit_theme_button' => [ '' ], |
| 108 |
'_srfm_additional_classes' => [ '' ], |
| 109 |
'_srfm_submit_type' => [ 'message' ], |
| 110 |
'_srfm_form_recaptcha' => [ 'none' ], |
| 111 |
]; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Create new form post from selected template. |
| 116 |
* |
| 117 |
* @param \WP_REST_Request $data Form Markup Data. |
| 118 |
* |
| 119 |
* @return WP_Error|WP_REST_Response |
| 120 |
* @since 0.0.1 |
| 121 |
*/ |
| 122 |
public static function create_form( $data ) { |
| 123 |
$nonce = Helper::get_string_value( $data->get_header( 'X-WP-Nonce' ) ); |
| 124 |
$nonce = sanitize_text_field( $nonce ); |
| 125 |
|
| 126 |
if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) { |
| 127 |
wp_send_json_error( |
| 128 |
[ |
| 129 |
'message' => __( 'Nonce verification failed.', 'sureforms' ), |
| 130 |
] |
| 131 |
); |
| 132 |
} |
| 133 |
|
| 134 |
$form_info = $data->get_body(); |
| 135 |
$form_info_obj = json_decode( $form_info ); |
| 136 |
|
| 137 |
// Check if JSON decoding was successful and $form_info_obj is an object. |
| 138 |
if ( json_last_error() !== JSON_ERROR_NONE || ! is_object( $form_info_obj ) ) { |
| 139 |
wp_send_json_error( |
| 140 |
[ |
| 141 |
'message' => __( 'Invalid JSON format.', 'sureforms' ), |
| 142 |
] |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
$required_properties = [ 'template_name', 'form_data' ]; |
| 147 |
|
| 148 |
// Check if required properties exist in the $form_info_obj. |
| 149 |
foreach ( $required_properties as $property ) { |
| 150 |
if ( ! property_exists( $form_info_obj, $property ) ) { |
| 151 |
wp_send_json_error( |
| 152 |
[ |
| 153 |
'message' => __( 'Missing required properties in form info.', 'sureforms' ), |
| 154 |
] |
| 155 |
); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
$title = isset( $form_info_obj->template_name ) ? $form_info_obj->template_name : ''; |
| 160 |
$content = isset( $form_info_obj->form_data ) ? $form_info_obj->form_data : ''; |
| 161 |
$template_metas = isset( $form_info_obj->template_metas ) ? (array) $form_info_obj->template_metas : []; |
| 162 |
|
| 163 |
// if post content contains srfm/page-break block, then set _srfm_is_page_break meta to true. |
| 164 |
if ( strpos( $content, 'srfm/page-break' ) !== false ) { |
| 165 |
$template_metas['_srfm_is_page_break'] = [ 'true' ]; |
| 166 |
} |
| 167 |
|
| 168 |
$post_id = wp_insert_post( |
| 169 |
[ |
| 170 |
'post_title' => $title, |
| 171 |
'post_content' => $content, |
| 172 |
'post_status' => 'draft', |
| 173 |
'post_type' => 'sureforms_form', |
| 174 |
] |
| 175 |
); |
| 176 |
|
| 177 |
if ( ! empty( $post_id ) ) { |
| 178 |
if ( ! empty( $template_metas ) ) { |
| 179 |
$default_post_metas = self::get_default_meta_keys(); |
| 180 |
$post_metas = array_merge( $default_post_metas, $template_metas ); |
| 181 |
|
| 182 |
foreach ( $post_metas as $meta_key => $meta_value ) { |
| 183 |
add_post_meta( $post_id, $meta_key, $meta_value[0] ); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
return new WP_REST_Response( |
| 188 |
[ |
| 189 |
'message' => __( 'SureForms Form created successfully.', 'sureforms' ), |
| 190 |
'id' => $post_id, |
| 191 |
] |
| 192 |
); |
| 193 |
} else { |
| 194 |
wp_send_json_error( |
| 195 |
[ |
| 196 |
'message' => __( 'Error creating SureForms Form, ', 'sureforms' ), |
| 197 |
] |
| 198 |
); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
} |
| 204 |
|