Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Created June 14, 2021 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wpmudev-sls/9e8fb7fc619e2e7aaab19688e598e3d1 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/9e8fb7fc619e2e7aaab19688e598e3d1 to your computer and use it in GitHub Desktop.
[Forminator] Encode Autofill On Redirection
<?php
/**
* Plugin Name: [Forminator] Encode Autofill On Redirection
* Plugin URI: https://premium.wpmudev.org/
* Description: When your form uses a URL redirection with an autofill field with the & character, the target form won't load the value after the & character. This plugin fixes this problem.
* Author: Glauber Silva @ WPMUDEV
* Author URI: https://wpmudev.com/
* Task: SLS-2236
* License: GPLv2 or later
*
* @package WPMUDEV_Forminator_Encode_Autofill
*/
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'WPMUDEV_Forminator_Encode_Autofill' ) ) {
/**
* Main class of the plugin.
*/
class WPMUDEV_Forminator_Encode_Autofill {
/**
* URL Field.
*
* @var Property
*/
private $autofill_field = null;
/**
* Stores the main instance of the class.
*
* @var Property
*/
private static $instance = null;
/**
* Returns the main instance of the class.
*
* @return WPMUDEV_Forminator_Encode_Autofill
*/
public static function get_instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new WPMUDEV_Forminator_Encode_Autofill();
}
return self::$instance;
}
/**
* Constructor of the class.
*/
private function __construct() {
$this->init();
}
/**
* Loads the functions of the class in the apropriete hooks.
*/
public function init() {
add_action( 'forminator_custom_form_submit_field_data', array( $this, 'get_autofill_field' ), 10, 2 );
add_filter( 'forminator_replace_form_data', array( $this, 'encode_autofill_value' ), 10, 3 );
}
/**
* Before submitting, check if the form has a field with the forminator-encode-autofill class.
*/
public function get_autofill_field( $field_data_array, $form_id ) {
$custom_form = Forminator_Form_Model::model()->load( $form_id );
$fields = $custom_form->get_fields();
foreach ($fields as $field) {
if ( isset( $field->raw['custom-class'] ) && 'forminator-encode-autofill' == $field->raw['custom-class'] ) {
foreach ($field_data_array as $key => $value) {
if ( $field->slug == $value['name'] ) {
$this->autofill_field = $value['name'];
}
}
}
}
return $field_data_array;
}
/**
* Replace the default autofill value by an encoded version.
*/
public function encode_autofill_value( $content, $data, $fields ) {
if ( isset( $data[ $this->autofill_field ] ) && strpos( $content, site_url() ) !== false ) {
$content = str_replace( $data[ $this->autofill_field ], urlencode( $data[ $this->autofill_field ] ), $content );
}
return $content;
}
}
add_action(
'plugins_loaded',
function() {
return WPMUDEV_Forminator_Encode_Autofill::get_instance();
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment