diff --git a/assets/css/admin.scss b/assets/css/admin.scss index 034039d7e..cf1d1672a 100644 --- a/assets/css/admin.scss +++ b/assets/css/admin.scss @@ -229,7 +229,7 @@ } } - /** + /** * Notice info */ .everest-forms-notice { @@ -9268,12 +9268,6 @@ display: flex; align-items: center; margin-bottom: 10px; - - @media screen and (max-width: 1260px) { - flex-direction: column; - gap: 8px; - align-items: flex-start; - } label { flex: 1; @@ -9282,7 +9276,6 @@ &--field { flex: 2; - width: 100%; } .evf-restapi-key { width: 530px !important; @@ -9365,10 +9358,6 @@ max-width: 100%; transition: all 0.3s; - @media screen and (max-width: 1300px) { - width: 220px; - } - &--top { text-align: center; padding-bottom: 30px; @@ -9742,14 +9731,9 @@ .everest-forms-recaptcha-type { margin: 0; display: flex; - flex-wrap: wrap; align-items: center; gap: 24px; - @media screen and (max-width: 1260px) { - gap: 16px; - } - li { margin-bottom: 0; @@ -10065,13 +10049,20 @@ &-premium { &-sidebar { - // width: 300px; - // max-width: 100%; - // padding: 18px 10px 26px 10px; - // transition: all 0.3s; + width: 300px; + max-width: 100%; + padding: 18px 10px 26px 10px; + transition: all 0.3s; h2 { color: #222222; + font-size: 18px; + font-weight: 600; + line-height: 140%; + margin: 0; + padding-bottom: 16px; + margin-bottom: 20px; + border-bottom: 1px solid #eee8f7; } p { @@ -10092,9 +10083,63 @@ margin-bottom: 12px; } + ul { + margin: 0; + margin-bottom: 16px; + display: flex; + flex-direction: column; + gap: 12px; + + li { + color: #383838; + font-size: 15px; + font-weight: 400; + line-height: 23px; + margin-bottom: 0; + padding-left: 26px; + position: relative; + + &::before { + content: ""; + background-image: url("../images/icons/premium-list-check-icon.svg"); + background-repeat: no-repeat; + background-position: center; + background-size: 100%; + width: 18px; + height: 18px; + position: absolute; + left: 0; + top: 2px; + } + } + } + + a { + border-radius: 3px; + border: 1px solid #7545bb; + background: #7545bb; + padding: 10px 16px; + display: block; + color: #ffffff; + font-size: 14px; + font-style: normal; + font-weight: 500; + line-height: 24px; + text-decoration: none; + letter-spacing: 0.05px; + width: max-content; + margin: 20px 0; + transition: all 0.3s; + + &:hover { + background: #9159e3; + border-color: #9159e3; + } + } + &.everest-forms-hidden { - // margin-right: -325px; - // display: unset; + margin-right: -325px; + display: unset; } } } @@ -10178,26 +10223,6 @@ } } - -body { - &.evf-premium-sidebar-hidden { - .everest-forms-settings-premium-sidebar { - margin-right: -885px; - display: unset; - } - } - - &.evf-premium-sidebar-show { - .everest-forms-settings-premium-sidebar { - display: block; - width: 300px; - max-width: 100%; - padding: 18px 10px 26px 10px; - transition: all .3s; - } - } -} - #everest-forms-dashboard { background-color: #FAFAFC; } diff --git a/assets/js/admin/admin.js b/assets/js/admin/admin.js index 1a08cbe8b..c406ea636 100644 --- a/assets/js/admin/admin.js +++ b/assets/js/admin/admin.js @@ -585,8 +585,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); + } + }) + }); diff --git a/includes/RestApi/class-evf-rest-api.php b/includes/RestApi/class-evf-rest-api.php index 9a9b43108..fbb5506f2 100644 --- a/includes/RestApi/class-evf-rest-api.php +++ b/includes/RestApi/class-evf-rest-api.php @@ -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' ) ); } @@ -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', ); } } diff --git a/includes/RestApi/controllers/version1/class-evf-entry-submission.php b/includes/RestApi/controllers/version1/class-evf-entry-submission.php new file mode 100644 index 000000000..01deb448d --- /dev/null +++ b/includes/RestApi/controllers/version1/class-evf-entry-submission.php @@ -0,0 +1,292 @@ +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 ) + ); + } +} diff --git a/includes/admin/class-evf-admin-assets.php b/includes/admin/class-evf-admin-assets.php index a3524bf41..039a333dc 100644 --- a/includes/admin/class-evf-admin-assets.php +++ b/includes/admin/class-evf-admin-assets.php @@ -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 ); @@ -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', diff --git a/includes/admin/class-evf-admin-settings.php b/includes/admin/class-evf-admin-settings.php index c5f6b690c..7adf695c9 100644 --- a/includes/admin/class-evf-admin-settings.php +++ b/includes/admin/class-evf-admin-settings.php @@ -870,6 +870,39 @@ class=" +
+ +
+ + +
+ generate'; + } else { + echo ''; + } + ?> +
+
+
+ 'no', 'desc_tip' => true, ), + array( + 'title' => esc_html__( 'Enable RestApi', 'everest-forms' ), + 'desc' => __( 'Allow the other to use the rest api.', 'everest-forms' ), + 'id' => 'everest_forms_enable_restapi', + 'type' => 'toggle', + 'default' => 'no', + 'desc_tip' => true, + ), + array( + 'title' => esc_html__( 'RestApi Key', 'everest-forms' ), + 'desc' => __( 'List of api key.These are used to authenticate the request.', 'everest-forms' ), + 'id' => 'everest_forms_restapi_keys', + 'type' => 'restapi_key', + 'default' => '', + 'desc_tip' => true, + 'css' => 'width=500px !important;', + 'class' => 'evf-restapi-key', + ), array( 'type' => 'sectionend', 'id' => 'misc_options', diff --git a/includes/class-evf-ajax.php b/includes/class-evf-ajax.php index d84deedd2..ca2d617ce 100644 --- a/includes/class-evf-ajax.php +++ b/includes/class-evf-ajax.php @@ -132,6 +132,7 @@ public static function add_ajax_events() { 'send_routine_report_test_email' => false, 'map_csv' => false, 'import_entries' => false, + 'generate_restapi_key' => false, ); foreach ( $ajax_events as $ajax_event => $nopriv ) { @@ -1669,6 +1670,24 @@ public static function import_entries() { ); } } + /** + * Generate the restapi key + * + * @since xx.xx.xx + */ + public static function generate_restapi_key() { + try { + check_ajax_referer( 'process-restapi-api-ajax-nonce', 'security' ); + $key = generate_api_key(); + wp_send_json_success( $key ); + } catch ( Exception $e ) { + wp_send_json_error( + array( + 'message' => $e->getMessage(), + ) + ); + } + } } EVF_AJAX::init(); diff --git a/includes/class-evf-form-task.php b/includes/class-evf-form-task.php index 0249ad1f1..43de6617e 100644 --- a/includes/class-evf-form-task.php +++ b/includes/class-evf-form-task.php @@ -1025,7 +1025,7 @@ public function entry_save( $fields, $entry, $form_id, $form_data = array() ) { $entry_id = false; $status = isset( $entry['evf_spam_status'] ) ? $entry['evf_spam_status'] : 'publish'; $admin_approval_entries = get_option( 'everest_forms_admin_approval_entries_enable', 'no' ); - $settings = $this->form_data['settings']; + $settings = isset( $form_data['settings'] ) ? $form_data['settings'] : array(); $evf_form_admin_approval_entries = isset( $settings['enable_admin_approval_entries'] ) ? $settings['enable_admin_approval_entries'] : '0'; if ( 'yes' === $admin_approval_entries && '1' === $evf_form_admin_approval_entries ) { diff --git a/includes/evf-core-functions.php b/includes/evf-core-functions.php index 77cac6b0b..8493f4903 100644 --- a/includes/evf-core-functions.php +++ b/includes/evf-core-functions.php @@ -5604,7 +5604,17 @@ function evf_get_next_key_array( $arr, $key ) { return isset( $next_key ) ? $next_key : '' ; } +/** + * Function to generate the api key base on the string. + * + * @since xx.xx.xx + * @param $string The string value. + */ +function generate_api_key( $string = 'evf_restapi', $length = 32 ) { + $key = bin2hex( random_bytes( $length ) ); + return $key; +} add_action( 'wp_mail_failed', 'evf_email_send_failed_handler', 1 );