Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - Entry submission RestApi #1322

Merged
merged 16 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20,120 changes: 10,059 additions & 10,061 deletions assets/css/admin.scss

Large diffs are not rendered by default.

43 changes: 41 additions & 2 deletions assets/js/admin/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,8 +561,47 @@
}
});
});


//Rest api settings.
if($('#everest_forms_enable_restapi').is(":checked")){
$(document).find('.evf-restapi-key-wrapper').show();
}else {
$(document).find('.evf-restapi-key-wrapper').hide();
}
$('#everest_forms_enable_restapi').on('click', function(e){
const {checked} = e.target;
if(checked) {
$(document).find('.evf-restapi-key-wrapper').show();
}else {
$(document).find('.evf-restapi-key-wrapper').hide();
}
});
$('#everest_forms_restapi_keys').on('click', function(e){
evfClearClipboard();
evfSetClipboard( $( this ).val(), $( this ) );
e.preventDefault();
}).on('aftercopy', function() {
$( this ).tooltipster( 'content', $( this ).attr( 'data-copied' ) ).trigger( 'mouseenter' ).on( 'mouseleave', function() {
var $this = $( this );

setTimeout( function() {
$this.tooltipster( 'content', $this.attr( 'data-tip' ) );
}, 5000 );
} );
});
$('.everest-forms-generate-api-key, .everest-forms-regenerate-api-key').on('click', function(){
let data = {
action: "everest_forms_generate_restapi_key",
security: everest_forms_admin_generate_restapi_key.ajax_restapi_key_nonce,
};
$.ajax({
url: everest_forms_admin_generate_restapi_key.ajax_url,
type: "post",
data:data,
success:(res)=>{
$(document).find('#everest_forms_restapi_keys').val(res.data);
}
})
});



Expand Down
4 changes: 4 additions & 0 deletions includes/RestApi/class-evf-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ class EVF_REST_API {
* @since 2.0.8.1
*/
public static function init() {
// For Internal.
include __DIR__ . '/controllers/version1/class-evf-modules.php';
include __DIR__ . '/controllers/version1/class-evf-changelog.php';
include __DIR__ . '/controllers/version1/class-evf-gutenberg-blocks.php';
// For external.
include __DIR__ . '/controllers/version1/class-evf-entry-submission.php';

add_action( 'rest_api_init', array( __CLASS__, 'register_rest_routes' ) );
}
Expand Down Expand Up @@ -90,6 +93,7 @@ protected static function get_v1_rest_classes() {
'modules' => 'EVF_Modules',
'changelog' => 'EVF_Changelog',
'gutenberg-blocks' => 'EVF_Gutenberg_Blocks',
'entry-submission' => 'EVF_Entry_Submission',
);
}
}
Expand Down
292 changes: 292 additions & 0 deletions includes/RestApi/controllers/version1/class-evf-entry-submission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,292 @@
<?php
/**
* Entry Submission Controller Class.
*
* @since xx.xx.xx
*
* @package EverestForms/Classes
*/

defined( 'ABSPATH' ) || exit;

/**
* UR_AddonsClass
*/
class EVF_Entry_Submission {

/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'everest-forms/v1';

/**
* Route base.
*
* @var string
*/
protected $rest_base = 'entry';

/**
* Register routes.
*
* @since xx.xx.xx
*
* @return void
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/save',
array(
'methods' => 'POST',
'callback' => array( $this, 'save_entry' ),
'permission_callback' => array( $this, 'check_permissions' ),
)
);
}
/**
* Save the entry.
*
* @since xx.xx.xx
* @param WP_REST_Request $request Full data about the request.
*/
public function save_entry( $request ) {
global $wpdb;

$entry = $request->get_params();
if ( empty( $entry['form_fields'] ) ) {
return new \WP_REST_Response(
array(
'message' => esc_html__( 'No entry data found!', 'everest-forms' ),
'data' => $entry,
),
400
);
}

$form_id = isset( $entry['id'] ) ? absint( $entry['id'] ) : 0;

if ( empty( $form_id ) ) {
return new \WP_REST_Response(
array(
'message' => esc_html__( 'Form id is missing!', 'everest-forms' ),
'data' => $entry,
),
400
);
}

$form = evf()->form->get( $form_id );

if ( empty( $form ) ) {
return new \WP_REST_Response(
array(
'message' => esc_html__( 'Form is not found!', 'everest-forms' ),
'data' => $entry,
),
400
);
}

$form_data = apply_filters( 'everest_forms_process_before_form_data', evf_decode( $form->post_content ), $entry );

if ( isset( $form_data['form_enabled'] ) && ! $form_data['form_enabled'] ) {
return new \WP_REST_Response(
array(
'message' => esc_html__( 'Form is disalbed!', 'everest-forms' ),
'data' => $entry,
),
400
);
}

if ( empty( $form_data['form_fields'] ) ) {
return new \WP_REST_Response(
array(
'message' => esc_html__( 'Form is empty!', 'everest-forms' ),
'data' => $entry,
),
400
);
}

if ( isset( $form_data['settings']['disabled_entries'] ) && '1' === $form_data['settings']['disabled_entries'] ) {
return new \WP_REST_Response(
array(
'message' => esc_html__( 'Save entris is enable! Please disable to save the entry.', 'everest-forms' ),
'data' => $entry,
),
400
);
}

$errors = array();
$form_fields = array();
$entry = apply_filters( 'everest_forms_process_before_save_entry', $entry, $form_data );

$form_data['entry'] = $entry;

foreach ( $entry['form_fields'] as $field_id => $field_value ) {
if ( array_key_exists( $field_id, $form_data['form_fields'] ) ) {
$field_type = $form_data['form_fields'][ $field_id ]['type'];
if ( 'signature' === $field_type ) {
$field_submit = isset( $field_value['signature_image'] ) ? $field_value['signature_image'] : '';
}

$exclude = array( 'title', 'html', 'captcha', 'image-upload', 'file-upload', 'divider', 'reset', 'recaptcha', 'hcaptcha', 'turnstile' );

if ( ! in_array( $field_type, $exclude, true ) ) {
$form_fields[ $field_id ] = array(
'name' => sanitize_text_field( $form_data['form_fields'][ $field_id ]['label'] ),
'value' => $field_value,
'id' => $field_id,
'type' => $field_type,
'meta_key' => $form_data['form_fields'][ $field_id ]['meta-key'],
);

if ( 'checkbox' === $field_type ) {
$form_fields[ $field_id ]['value'] = array(
'name' => sanitize_text_field( $form_data['form_fields'][ $field_id ]['label'] ),
'type' => $field_type,
'label' => $field_value,
);
$form_fields[ $field_id ]['value_raw'] = $field_value;
}

if ( 'likert' === $field_type ) {
$likert_rows = $form_data['form_fields'][ $field_id ]['likert_rows'];
$likert_columns = $form_data['form_fields'][ $field_id ]['likert_columns'];
$combined_value = '';
foreach ( $field_value as $key => $value ) {
if ( array_key_exists( $key, $likert_rows ) ) {

$combined_value .= "$likert_rows[$key]:\n";
}
if ( array_key_exists( $key, $likert_columns ) ) {

$combined_value .= "$likert_columns[$key]:\n";
}
}
$form_fields[ $field_id ]['value'] = $combined_value;
$form_fields[ $field_id ]['value_raw'] = $field_value;
}
if ( 'wysiwyg' === $field_type ) {
$form_fields[ $field_id ]['value'] = wp_strip_all_tags( $field_value );
$form_fields[ $field_id ]['value_raw'] = $field_value;

}

if ( 'address' === $field_type ) {
$form_fields[ $field_id ]['value'] = implode( " \n ", $field_value );
}

if ( 'country' === $field_type ) {
$form_fields[ $field_id ]['value'] = array(
'type' => $field_type,
'country_code' => $field_value,
);
}
}
}
}
// Validate fields.
foreach ( $form_data['form_fields'] as $field ) {
$field_id = $field['id'];
$field_type = $field['type'];

$field_value = isset( $entry['form_fields'][ $field_id ] ) ? $entry['form_fields'][ $field_id ] : '';
do_action( "everest_forms_process_validate_{$field_type}", $field_id, $field_value, $form_data, $field_type );

}

$errors = isset( evf()->task->errors[ $form_data['id'] ] ) ? evf()->task->errors[ $form_data['id'] ] : array();

if ( ! empty( $errors ) ) {
return new \WP_REST_Response(
array(
'message' => esc_html__( 'Error found!!', 'everest-forms' ),
'errors' => $errors,
),
400
);
}

$task_instance = new EVF_Form_Task();
$entry_id = $task_instance->entry_save( $form_fields, $entry, $form_data['id'], $form_data );
/**
* Allow to send the email after save entry using rest api.
*
* @since xx.xx.xx
*
* @param boolean $allow The allow value.
*/
if ( $entry_id && apply_filters( 'everest_forms_allow_send_email_after_restapi_save_entry', false ) ) {
$task_instance->entry_email( $form_fields, $entry, $form_data, $entry_id, 'entry' );
}

return new \WP_REST_Response(
array(
'entry_id' => $entry_id,
),
200
);
}

/**
* Check if a given request has access to update a setting
*
* @param WP_REST_Request $request Full data about the request.
* @return WP_Error|bool
*/
public function check_permissions( $request ) {
$enable_rest_api = get_option( 'everest_forms_enable_restapi', false );

if ( ! evf_string_to_bool( $enable_rest_api ) ) {
return new \WP_Error(
'unauthorized',
esc_html__( 'Contact your administrator to enable REST API access', 'everest-forms' ),
array( 'status' => 401 )
);
}

$api_key = get_option( 'everest_forms_restapi_keys', '' );

if ( '' === $api_key ) {
return new \WP_Error(
'unauthorized',
esc_html__( 'Contact your administrator to generate the api key.', 'everest-forms' ),
array( 'status' => 401 )
);
}

$headers = $request->get_headers();

if ( ! isset( $headers['api_key'] ) ) {
return new \WP_Error(
'unauthorized',
esc_html__( 'Missing api key!', 'everest-forms' ),
array( 'status' => 401 )
);
}

if ( ! isset( $headers['api_key'][0] ) || empty( $headers['api_key'][0] ) ) {
return new \WP_Error(
'unauthorized',
esc_html__( 'Empty api key!', 'everest-forms' ),
array( 'status' => 401 )
);
}

if ( $headers['api_key'][0] === $api_key ) {
return true;
}

return new \WP_Error(
'unauthorized',
esc_html__( 'Unauthorized api key.', 'everest-forms' ),
array( 'status' => 401 )
);
}
}
11 changes: 10 additions & 1 deletion includes/admin/class-evf-admin-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function admin_scripts() {
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';

// Register scripts.
wp_register_script( 'everest-forms-admin', evf()->plugin_url() . '/assets/js/admin/admin' . $suffix . '.js', array( 'jquery', 'jquery-blockui', 'jquery-ui-sortable', 'jquery-ui-widget', 'jquery-ui-core', 'tooltipster', 'wp-color-picker', 'perfect-scrollbar' ), EVF_VERSION, true );
wp_register_script( 'everest-forms-admin', evf()->plugin_url() . '/assets/js/admin/admin' . $suffix . '.js', array( 'jquery', 'jquery-blockui', 'jquery-ui-sortable', 'jquery-ui-widget', 'jquery-ui-core', 'tooltipster', 'wp-color-picker', 'perfect-scrollbar', 'evf-clipboard' ), EVF_VERSION, true );
wp_register_script( 'everest-forms-extensions', evf()->plugin_url() . '/assets/js/admin/extensions' . $suffix . '.js', array( 'jquery', 'updates', 'wp-i18n' ), EVF_VERSION, true );
wp_register_script( 'everest-forms-email-admin', evf()->plugin_url() . '/assets/js/admin/evf-admin-email' . $suffix . '.js', array( 'jquery', 'jquery-blockui', 'jquery-ui-sortable', 'jquery-ui-widget', 'jquery-ui-core', 'tooltipster', 'wp-color-picker', 'perfect-scrollbar' ), EVF_VERSION, true );
wp_register_script( 'everest-forms-editor', evf()->plugin_url() . '/assets/js/admin/editor' . $suffix . '.js', array( 'jquery' ), EVF_VERSION, true );
Expand Down Expand Up @@ -303,6 +303,15 @@ public function admin_scripts() {
)
);

wp_localize_script(
'everest-forms-admin',
'everest_forms_admin_generate_restapi_key',
array(
'ajax_restapi_key_nonce' => wp_create_nonce( 'process-restapi-api-ajax-nonce' ),
'ajax_url' => admin_url( 'admin-ajax.php', 'relative' ),
)
);

wp_localize_script(
'everest-forms-admin',
'everest_forms_admin_form_migrator',
Expand Down
Loading
Loading