Last active
February 25, 2023 22:09
-
-
Save patrickfreitasdev/b9bc1aaa68b96e539c543b13db988c9a to your computer and use it in GitHub Desktop.
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] - Add support for limit minimum Forminator phone fields characters | |
* Description: [Forminator] - Add support for limit minimum Forminator phone fields characters | |
* Based on https://gist.github.com/wpmudev-sls/b20040d0f49fb822553194fd0a34d5ff | |
* Author: Thobk @ WPMUDEV | Patrick Freitas @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { exit; } elseif ( defined( 'WP_CLI' ) && WP_CLI ) { return; } | |
add_action( 'after_setup_theme', 'wpmudev_forminator_minimum_phone_field_length', 100 ); | |
function wpmudev_forminator_minimum_phone_field_length() { | |
if ( defined('FORMINATOR_PRO') && class_exists( 'Forminator' ) ) { | |
class WPMUDEV_Forminator_Phone_Required_Length{ | |
private $field_ids = ['phone-1' => 2, 'textarea-1' => 30];// Enter field ids: {field-id} => {minimum characters}, e.g: ["text-1" => 15, "textarea-1" => 30] | |
private $form_ids = [];//add list form ids here, default this will apply for all forms | |
private $exclude_form_ids = [];//add list exclude form ids here, [345, 456] | |
private $valid_field = false; | |
private $min_value; | |
private $form_fields; | |
private $activated_on_this_form; | |
private $current_form_id; | |
private $invalid_input; | |
public function __construct() | |
{ | |
add_action( 'forminator_form_post_message', array( $this, 'should_activate_on_this_form') ); | |
add_filter( 'forminator_custom_form_submit_errors', array( $this, 'check_input_length' ), 10, 3 ); | |
add_filter( 'forminator_custom_form_invalid_form_message', array( $this, 'custom_message' ) ); | |
add_filter( 'forminator_field_phone_markup', array( $this, 'maybe_add_min_length_attr'), 10, 2 ); | |
} | |
public function get_fields( $form_id ){ | |
if( ! $this->form_fields ){ | |
$custom_form = Forminator_Form_Model::model()->load( $form_id ); | |
$form_fields = $custom_form->get_fields(); | |
foreach( $form_fields as $field ){ | |
$this->form_fields[ $field->slug ] = $field->__get('field_label'); | |
} | |
} | |
return $this->form_fields; | |
} | |
public function should_activate_on_this_form( $form_id ){ | |
$this->current_form_id = $form_id; | |
if( ! isset( $this->activated_on_this_form[ $form_id ] ) ){ | |
$this->activated_on_this_form[ $form_id ] = true; | |
if( $this->form_ids ){ | |
if( ! in_array( $form_id, $this->form_ids ) ){ | |
$this->activated_on_this_form[ $form_id ] = null; | |
} | |
}elseif( $this->exclude_form_ids && in_array( $form_id, $this->exclude_form_ids ) ){ | |
$this->activated_on_this_form[ $form_id ] = null; | |
} | |
} | |
return $this->activated_on_this_form[ $form_id ]; | |
} | |
public function get_field_label( $field_id, $form_id ){ | |
$form_fields = $this->get_fields( $form_id ); | |
return isset( $form_fields[ $field_id ] ) ? $form_fields[ $field_id ] : $field_id; | |
} | |
public function check_input_length( $submit_errors, $form_id, $field_data_array ){ | |
if( $submit_errors || ! $this->should_activate_on_this_form( $form_id ) ){ | |
return $submit_errors; | |
} | |
if( ! empty( $this->field_ids ) ){ | |
foreach( $field_data_array as $field ){ | |
if( isset( $this->field_ids[ $field['name'] ] ) && $this->field_ids[ $field['name'] ] > strlen( $field['value'] ) ){ | |
$submit_errors[ $field['name'] ] = $field['name']; | |
$this->invalid_input = array( | |
'field' => $this->get_field_label( $field['name'], $form_id ), | |
'minlength' => $this->field_ids[ $field['name'] ] | |
); | |
break; | |
} | |
} | |
} | |
return $submit_errors; | |
} | |
public function maybe_add_min_length_attr( $html, $id ){ | |
foreach( $this->field_ids as $key => $value ){ | |
if ( strpos( $id, $value ) !== false ) { | |
$this->valid_field = true; | |
$this->min_value = $value; | |
} | |
} | |
if( $this->current_form_id && isset( $this->activated_on_this_form[ $this->current_form_id ] ) && $this->valid_field ){ | |
$name = 'name="'. $id .'"'; | |
$html = str_replace( $name, $name .' minlength="'. $this->min_value .'"', $html ); | |
} | |
return $html; | |
} | |
public function custom_message($message){ | |
if( $this->invalid_input ){ | |
$message = sprintf('You need to insert at least %d characters in the %s field', $this->invalid_input['minlength'], $this->invalid_input['field'] ); | |
$this->invalid_input = null; | |
$this->form_fields = null; | |
} | |
return $message; | |
} | |
} | |
$run = new WPMUDEV_Forminator_Phone_Required_Length; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment