plugin updates
This commit is contained in:
@@ -0,0 +1,212 @@
|
||||
<?php
|
||||
/**
|
||||
* Handles AJAX services such as validation and submission.
|
||||
*
|
||||
* @package Gravity_Forms\Gravity_Forms\Ajax
|
||||
*/
|
||||
namespace Gravity_Forms\Gravity_Forms\Ajax;
|
||||
|
||||
/**
|
||||
* Class GF_Ajax_Handler
|
||||
*
|
||||
* @since 2.9.0
|
||||
*
|
||||
* Provides functionality for handling AJAX validation and submission.
|
||||
*/
|
||||
class GF_Ajax_Handler {
|
||||
|
||||
/**
|
||||
* Handles the form validation AJAX requests. Uses the global $_POST array and sends the form validation result as a JSON response.
|
||||
*
|
||||
* @since 2.9.0
|
||||
*/
|
||||
public function validate_form() {
|
||||
|
||||
// Check nonce.
|
||||
$nonce_result = check_ajax_referer( 'gform_ajax_submission', 'gform_ajax_nonce', false );
|
||||
|
||||
if ( ! $nonce_result ) {
|
||||
wp_send_json_error( $this->nonce_validation_message() );
|
||||
}
|
||||
|
||||
$form_id = absint( rgpost( 'form_id' ) );
|
||||
$target_page = absint( rgpost( 'gform_target_page_number_' . $form_id ) );
|
||||
$source_page = absint( rgpost( 'gform_source_page_number_' . $form_id ) );
|
||||
|
||||
$result = \GFAPI::validate_form( $form_id, array(), rgpost( 'gform_field_values' ), $target_page, $source_page );
|
||||
if ( is_wp_error( $result ) ) {
|
||||
wp_send_json_error( $result->get_error_message() );
|
||||
}
|
||||
|
||||
$form = $result['form'];
|
||||
if ( ! $result['is_valid'] ) {
|
||||
$result = $this->add_validation_summary( $form, $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the form validation result.
|
||||
*
|
||||
* @since 2.9.0
|
||||
*
|
||||
* @param array $result The form validation result to be filtered.
|
||||
*
|
||||
* @return array The filtered form validation result.
|
||||
*/
|
||||
$result = gf_apply_filters( array( 'gform_ajax_validation_result', $form['id'] ), $result );
|
||||
|
||||
// Remove form from result.
|
||||
unset( $result['form'] );
|
||||
|
||||
wp_send_json_success( $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the form submission AJAX requests. Uses the global $_POST array and sends the form submission result as a JSON response.
|
||||
*
|
||||
* @since 2.9.0
|
||||
*/
|
||||
public function submit_form() {
|
||||
|
||||
// Check nonce.
|
||||
$nonce_result = check_ajax_referer( 'gform_ajax_submission', 'gform_ajax_nonce', false );
|
||||
|
||||
if ( ! $nonce_result ) {
|
||||
wp_send_json_error( $this->nonce_validation_message() );
|
||||
}
|
||||
|
||||
$form_id = absint( rgpost( 'form_id' ) );
|
||||
|
||||
/**
|
||||
* Allows actions to be performed right before an AJAX form submission.
|
||||
*
|
||||
* @since 2.9.0
|
||||
*
|
||||
* @param int $form_id The form ID.
|
||||
*/
|
||||
gf_do_action( array( 'gform_ajax_pre_submit_form', $form_id ), $form_id );
|
||||
|
||||
// Handling the save link submission.
|
||||
if ( isset( $_POST['gform_send_resume_link'] ) ) {
|
||||
$this->submit_save_link();
|
||||
return;
|
||||
}
|
||||
|
||||
$target_page = absint( rgpost( 'gform_target_page_number_' . $form_id ) );
|
||||
$source_page = absint( rgpost( 'gform_source_page_number_' . $form_id ) );
|
||||
|
||||
$result = \GFAPI::submit_form( $form_id, array(), rgpost( 'gform_field_values' ), $target_page, $source_page );
|
||||
|
||||
if ( is_wp_error( $result ) ) {
|
||||
wp_send_json_error( $result->get_error_message() );
|
||||
}
|
||||
|
||||
$form = $result['form'];
|
||||
|
||||
// Adding validation markup if form failed validation
|
||||
if ( ! $result['is_valid'] ) {
|
||||
$result = $this->add_validation_summary( $form, $result );
|
||||
}
|
||||
|
||||
// Adding confirmation markup if there is a confirmation message to be displayed.
|
||||
if ( rgar( $result, 'confirmation_type' ) == 'message' && ! empty( rgar( $result, 'confirmation_message' ) ) ) {
|
||||
$result['confirmation_markup'] = \GFFormDisplay::get_form( $form_id, false, false, false, rgpost( 'gform_field_values' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the ajax form submission result.
|
||||
*
|
||||
* @since 2.9.0
|
||||
*
|
||||
* @param array $result The form submission result to be filtered.
|
||||
*
|
||||
* @return array The filtered form submission result.
|
||||
*/
|
||||
$result = gf_apply_filters( array( 'gform_ajax_submission_result', $form_id ), $result );
|
||||
|
||||
// Remove form from result.
|
||||
unset( $result['form'] );
|
||||
|
||||
wp_send_json_success( $result );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Handles the save link submission. Uses the $_POST array and sends the save link result as a JSON response.
|
||||
*
|
||||
* @since 2.9.0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function submit_save_link() {
|
||||
$form_id = absint( rgpost( 'form_id' ) );
|
||||
|
||||
\GFFormDisplay::process_send_resume_link();
|
||||
|
||||
$confirmation = \GFFormDisplay::get_form( $form_id, false, false, false, rgpost( 'gform_field_values' ) );
|
||||
|
||||
wp_send_json_success(
|
||||
array(
|
||||
'is_valid' => true,
|
||||
'confirmation_type' => 'message',
|
||||
'confirmation_message' => $confirmation,
|
||||
'confirmation_markup' => $confirmation,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the lifespan of the nonce used for AJAX submissions and validation.
|
||||
*
|
||||
* @since 2.9.0
|
||||
*
|
||||
* @param int $lifespan_in_seconds The lifespan of the nonce in seconds. Defaults to 3 days
|
||||
* @param string $action The nonce action (gform_ajax_submission or gform_ajax_validation).
|
||||
*
|
||||
* @return int The filtered lifespan of the nonce in seconds.
|
||||
*/
|
||||
public function nonce_life( $lifespan_in_seconds, $action = '' ) {
|
||||
if ( in_array( $action, array( 'gform_ajax_submission', 'gform_ajax_validation' ) ) ) {
|
||||
|
||||
/**
|
||||
* Filters the lifespan of the nonce used for AJAX submissions and validation.
|
||||
*
|
||||
* @since 2.9.0
|
||||
*
|
||||
* @param int $lifespan_in_seconds The lifespan of the nonce in seconds (defaults to 3 days).
|
||||
* @param string $action The nonce action (gform_ajax_submission or gform_ajax_validation).
|
||||
*
|
||||
* @return int The lifespan of the nonce in seconds.
|
||||
*/
|
||||
$lifespan_in_seconds = apply_filters( 'gform_nonce_life', 3 * DAY_IN_SECONDS, $action );
|
||||
}
|
||||
|
||||
return $lifespan_in_seconds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the nonce validation message.
|
||||
*
|
||||
* @since 2.9.0
|
||||
*
|
||||
* @return string The nonce validation message.
|
||||
*/
|
||||
private function nonce_validation_message() {
|
||||
return esc_html__( 'Your session has expired. Please refresh the page and try again.', 'gravityforms' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the validation summary properties to the form validation result.
|
||||
*
|
||||
* @since 2.9.0
|
||||
*
|
||||
* @param array $form The form being validated.
|
||||
* @param array $result The form validation result.
|
||||
*
|
||||
* @return mixed Returns the form validation result with the validation summary properties added.
|
||||
*/
|
||||
private function add_validation_summary( $form, $result ) {
|
||||
$summary = \GFFormDisplay::get_validation_errors_markup( $form, array(), rgar( $form, 'validationSummary' ) );
|
||||
$result['validation_summary'] = wp_kses( $summary, wp_kses_allowed_html( 'post' ) );
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
/**
|
||||
* Service Provider for AJAX Service
|
||||
*
|
||||
* @package Gravity_Forms\Gravity_Forms\Ajax
|
||||
*/
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Ajax;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Config\GF_Config_Service_Provider;
|
||||
use Gravity_Forms\Gravity_Forms\GF_Service_Container;
|
||||
use Gravity_Forms\Gravity_Forms\GF_Service_Provider;
|
||||
use Gravity_Forms\Gravity_Forms\Ajax\Config\GF_Ajax_Config;
|
||||
|
||||
/**
|
||||
* Class GF_Ajax_Service_Provider
|
||||
*
|
||||
* Service provider for the Ajax Service.
|
||||
*/
|
||||
class GF_Ajax_Service_Provider extends GF_Service_Provider {
|
||||
|
||||
const GF_AJAX_HANDLER = 'gf_ajax_handler';
|
||||
const GF_AJAX_CONFIG = 'gf_ajax_config';
|
||||
|
||||
/**
|
||||
* Includes all related files and adds all containers.
|
||||
*
|
||||
* @param GF_Service_Container $container Container singleton object.
|
||||
*/
|
||||
public function register( GF_Service_Container $container ) {
|
||||
|
||||
require_once plugin_dir_path( __FILE__ ) . 'class-gf-ajax-handler.php';
|
||||
require_once plugin_dir_path( __FILE__ ) . 'config/class-gf-ajax-config.php';
|
||||
|
||||
// Registering handler
|
||||
$container->add(
|
||||
self::GF_AJAX_HANDLER,
|
||||
function () {
|
||||
return new GF_Ajax_Handler();
|
||||
}
|
||||
);
|
||||
|
||||
// Registering config
|
||||
$container->add(
|
||||
self::GF_AJAX_CONFIG,
|
||||
function () use ( $container ) {
|
||||
return new GF_Ajax_Config( $container->get( GF_Config_Service_Provider::DATA_PARSER ) );
|
||||
}
|
||||
);
|
||||
$container->get( GF_Config_Service_Provider::CONFIG_COLLECTION )->add_config( $container->get( self::GF_AJAX_CONFIG ) );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes service.
|
||||
*
|
||||
* @param GF_Service_Container $container Service Container.
|
||||
*/
|
||||
public function init( GF_Service_Container $container ) {
|
||||
parent::init( $container );
|
||||
|
||||
$ajax_handler = $container->get( self::GF_AJAX_HANDLER );
|
||||
|
||||
// Register nonce lifespan hook.
|
||||
add_filter( 'nonce_life', array( $ajax_handler, 'nonce_life' ), 10, 2 );
|
||||
|
||||
// Register AJAX validation.
|
||||
add_action( 'wp_ajax_gform_validate_form', array( $ajax_handler, 'validate_form' ) );
|
||||
add_action( 'wp_ajax_nopriv_gform_validate_form', array( $ajax_handler, 'validate_form' ) );
|
||||
|
||||
// Register AJAX submission.
|
||||
add_action( 'wp_ajax_gform_submit_form', array( $ajax_handler, 'submit_form' ) );
|
||||
add_action( 'wp_ajax_nopriv_gform_submit_form', array( $ajax_handler, 'submit_form' ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Gravity_Forms\Gravity_Forms\Ajax\Config;
|
||||
|
||||
use Gravity_Forms\Gravity_Forms\Config\GF_Config;
|
||||
use GFForms;
|
||||
|
||||
/**
|
||||
* Config items for Ajax operations
|
||||
*
|
||||
* @since 2.9.0
|
||||
*/
|
||||
class GF_Ajax_Config extends GF_Config {
|
||||
|
||||
protected $name = 'gform_theme_config';
|
||||
protected $script_to_localize = 'gform_gravityforms_theme';
|
||||
|
||||
/**
|
||||
* Config data.
|
||||
*
|
||||
* @return array[]
|
||||
*/
|
||||
public function data() {
|
||||
$preview_query_string = \GFCommon::is_preview() ? '?gf_ajax_page=preview' : '';
|
||||
return array(
|
||||
'common' => array(
|
||||
'form' => array(
|
||||
'ajax' => array(
|
||||
'ajaxurl' => admin_url( 'admin-ajax.php' ) . $preview_query_string,
|
||||
'ajax_submission_nonce' => wp_create_nonce( 'gform_ajax_submission' ),
|
||||
'i18n' => array(
|
||||
/* Translators: This is used to announce the current step of a multipage form, 1. first step number, 2. total steps number, example: Step 1 of 5 */
|
||||
'step_announcement' => esc_html__( 'Step %1$s of %2$s, %3$s', 'gravityforms' ),
|
||||
'unknown_error' => esc_html__( 'There was an unknown error processing your request. Please try again.', 'gravityforms' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user