-
-
Save glaubersilva/61f3eabd623a47b61b97666e285a4e59 to your computer and use it in GitHub Desktop.
[Forminator] Encode Autofill On Redirection
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: [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