Last active
July 8, 2020 11:47
-
-
Save pradeep910/b25525034289ab0bc2b7fa0896f4ac90 to your computer and use it in GitHub Desktop.
Fix for gravity form submission with page update in AMP native mode (amp-wp plugin)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Gravity form fix for AMP-WP plugin | |
* Description: Fix for gravity form submission with page update in AMP native mode (amp-wp plugin). Gravity forms submission with page update fix for AMP Native mode of amp-wp plugin. https://ampbyexample.com/components/amp-form/#form-submission-with-page-update | |
* Plugin URI: https://gist.github.com/pradeep910/b25525034289ab0bc2b7fa0896f4ac90/ | |
* Version: 0.1 | |
* Author: Pradeep Sonawane | |
* Author URI: https://pradeepsonawane.com | |
* | |
* @package rtCamp | |
*/ | |
if ( ! function_exists( 'rt_is_amp_native' ) ) { | |
/** | |
* Check if AMP native is enabled. | |
* | |
* @return bool | |
*/ | |
function rt_is_amp_native() { | |
return ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) ? true : false; | |
} | |
} | |
/** | |
* Handle gravity form validation errors. | |
* | |
* @param array $validation Validation array. | |
* | |
* @return array | |
*/ | |
function rt_gform_handle_validation_error( $validation ) { | |
if ( empty( $validation['is_valid'] ) && rt_is_amp_native() ) { | |
if ( ! empty( $validation['form'] ) && ! empty( $validation['form']['fields'] ) ) { | |
foreach ( $validation['form']['fields'] as $fields ) { | |
if ( '' !== $fields['validation_message'] ) { | |
// Add error messages as string. | |
$errors .= sprintf( | |
"<p><span class='error-field-name'>%s</span> <span class='error-field-message'>%s</span></p>", | |
$fields['label'], | |
str_replace( 'This', '', $fields['validation_message'] ) | |
); | |
} | |
} | |
} | |
wp_send_json( array( | |
'error' => $errors | |
), 300 ); | |
exit(); | |
} | |
return $validation; | |
} | |
add_filter( 'gform_validation', 'rt_gform_handle_validation_error' ); | |
/** | |
* Support for gravity form submission with page update. https://ampbyexample.com/components/amp-form/#form-submission-with-page-update | |
* | |
* @param object $entry The entry that was just created via gravity form. | |
* | |
* @param array $form The current form. | |
*/ | |
function rt_redirect_gform_post_submission( $entry, $form) { | |
if ( rt_is_amp_native() ) { | |
$message = empty( $form['confirmation']['message'] ) ? sprintf( 'Thanks, this form has been submitted successfully!', 'rtcamp' ) : $form['confirmation']['message']; | |
// Send gravity form confirmation message as success message via json. | |
wp_send_json( array( | |
'success' => $message | |
) ); | |
exit(); | |
} | |
} | |
add_action( 'gform_after_submission', 'rt_redirect_gform_post_submission', 10, 2 ); | |
/** | |
* Filter Gravity form markup to add submit-success element. | |
* | |
* @param string $form_string Gravity form markup. | |
* @param array $form Gravity form fields. | |
* @return string | |
*/ | |
function rt_amp_gravity_form_filter( $form_string, $form ) { | |
if( rt_is_amp_native() && ! strpos( $form_string, 'submit-success' ) ) { | |
$form_string = str_replace( | |
'</form>', | |
sprintf( | |
'<div submit-success> | |
<template type="amp-mustache"> | |
{{{success}}} | |
</template> | |
</div>', | |
'rtcamp' | |
), | |
$form_string | |
); | |
} | |
return $form_string; | |
} | |
add_filter( 'gform_get_form_filter', 'rt_amp_gravity_form_filter', 10, 2 ); | |
/** | |
* Filters gravity form ajax attribute to disable ajax. Props Divyaraj Masani (@divyarajmasani) | |
* | |
* @param string $args post content. | |
* @return array | |
*/ | |
function rt_no_ajax_on_all_forms( $args ) { | |
if( rt_is_amp_native() ) { | |
$args['ajax'] = false; | |
} | |
return $args; | |
} | |
add_filter('gform_form_args', 'rt_no_ajax_on_all_forms', 10, 1); | |
// Use client-side validation UI instead of showing validation errors after page reload. Props Rahul Bansal at rtCamp. | |
add_filter( 'gform_field_content', function( $content, $field ) { | |
if ( $field->isRequired && rt_is_amp_native() ) { | |
return str_replace( 'aria-required=', 'required=', $content ); | |
} | |
return $content; | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment