Skip to content
Snippets Groups Projects
Commit c39db30f authored by Lucas García's avatar Lucas García
Browse files

feat: custom fields

parent cf8230a2
No related branches found
No related tags found
No related merge requests found
Showing
with 283 additions and 40 deletions
<?php
namespace WPCT_ERP_FORMS\Fields;
use Exception;
class BaseField
{
private static $instances = [];
public static function get_instance()
{
$cls = static::class;
if (!isset(self::$instances[$cls])) {
self::$instances[$cls] = new static();
}
return self::$instances[$cls];
}
public function register()
{
throw new Exception('Method to overwrite by inheritance');
}
}
<?php
namespace WPCT_ERP_FORMS\IBAN_Field;
namespace WPCT_ERP_FORMS\Fields\GF\Iban;
use GFForms;
use GFAddOn;
......@@ -10,7 +10,7 @@ GFForms::include_addon_framework();
class Addon extends GFAddOn
{
protected $_version = WPCT_ERP_FORMS_IBAN_FIELD_VERSION;
protected $_version = '1.0';
protected $_slug = 'wpct-erp-forms-iban-field';
protected $_title = 'Gravity Forms IBAN validated text field';
protected $_short_title = 'IBAN field';
......@@ -45,7 +45,7 @@ class Addon extends GFAddOn
class_exists('GF_Field') &&
class_exists('GF_Fields')
) {
GF_Fields::register(new Field());
GF_Fields::register(new GFField());
}
}
}
<?php
namespace WPCT_ERP_FORMS\IBAN_Field;
namespace WPCT_ERP_FORMS\Fields\GF\Iban;
use Exception;
use GF_Field;
class Field extends GF_Field
class GFField extends GF_Field
{
/**
* @var array @type Country codes
......
<?php
namespace WPCT_ERP_FORMS\Fields\GF\Iban;
use WPCT_ERP_FORMS\Fields\BaseField;
use GFAddOn;
class Field extends BaseField
{
public function register()
{
add_action('gform_loaded', [$this, '_register']);
}
private function _register()
{
if (!method_exists('GFForms', 'include_addon_framework')) return;
require_once 'Addon.php';
require_once 'Field.php';
GFAddOn::register(Addon::class);
}
}
<?php
require_once 'iban/index.php';
<?php
require_once 'BaseField.php';
if (defined('WPCF7_VERSION')) {
require_once 'wpcf7/index.php';
}
if (class_exists('GFForms')) {
require_once 'gf/index.php';
}
<?php
namespace WPCT_ERP_FORMS\Fields\WPCF7\Iban;
use WPCT_ERP_FORMS\Fields\BaseField;
use Exception;
class Field extends BaseField
{
/**
* @var array @type Country codes
*/
private $_countries = [
'al' => 28,
'ad' => 24,
'at' => 20,
'az' => 28,
'bh' => 22,
'be' => 16,
'ba' => 20,
'br' => 29,
'bg' => 22,
'cr' => 21,
'hr' => 21,
'cy' => 28,
'cz' => 24,
'dk' => 18,
'do' => 28,
'ee' => 20,
'fo' => 18,
'fi' => 18,
'fr' => 27,
'ge' => 22,
'de' => 22,
'gi' => 23,
'gr' => 27,
'gl' => 18,
'gt' => 28,
'hu' => 28,
'is' => 26,
'ie' => 22,
'il' => 23,
'it' => 27,
'jo' => 30,
'kz' => 20,
'kw' => 30,
'lv' => 21,
'lb' => 28,
'li' => 21,
'lt' => 20,
'lu' => 20,
'mk' => 19,
'mt' => 31,
'mr' => 27,
'mu' => 30,
'mc' => 27,
'md' => 24,
'me' => 22,
'nl' => 18,
'no' => 15,
'pk' => 24,
'ps' => 29,
'pl' => 28,
'pt' => 25,
'qa' => 29,
'ro' => 24,
'sm' => 27,
'sa' => 24,
'rs' => 22,
'sk' => 24,
'si' => 19,
'es' => 24,
'se' => 24,
'ch' => 21,
'tn' => 24,
'tr' => 26,
'ae' => 23,
'gb' => 22,
'vg' => 24,
];
/**
* @var array $type Char codes
*/
private $_chars = [
'a' => 10,
'b' => 11,
'c' => 12,
'd' => 13,
'e' => 14,
'f' => 15,
'g' => 16,
'h' => 17,
'i' => 18,
'j' => 19,
'k' => 20,
'l' => 21,
'm' => 22,
'n' => 23,
'o' => 24,
'p' => 25,
'q' => 26,
'r' => 27,
's' => 28,
't' => 29,
'u' => 30,
'v' => 31,
'w' => 32,
'x' => 33,
'y' => 34,
'z' => 35
];
public function register()
{
add_action('wpcf7_init', function () {
$this->_register();
});
}
private function _register()
{
wpcf7_add_form_tag(
['iban', 'iban*'],
function ($tag) {
return $this->handler($tag);
},
['name-attr' => true]
);
add_filter('wpcf7_validate_iban*', function ($result, $tag) {
return $this->validate($result, $tag);
}, 20, 2);
add_filter('wpcf7_validate_iban', function ($result, $tag) {
return $this->validate($result, $tag);
}, 20, 2);
}
private function handler($tag)
{
$atts = [
'type' => 'text',
'name' => $tag->name,
];
$input = sprintf('<input %s />', wpcf7_format_atts($atts));
return $input;
}
private function validate($result, $tag)
{
$value = $_POST[$tag->name];
try {
if (strlen($value) < 5) throw new Exception();
$value = strtolower(str_replace(' ', '', $value));
$country_exists = array_key_exists(substr($value, 0, 2), $this->_countries);
$country_conform = strlen($value) == $this->_countries[substr($value, 0, 2)];
if (!($country_exists && $country_conform)) throw new Exception();
$moved_char = substr($value, 4) . substr($value, 0, 4);
$move_char_array = str_split($moved_char);
$new_string = '';
foreach ($move_char_array as $key => $val) {
if (!is_numeric($move_char_array[$key])) {
if (!isset($this->_chars[$val])) throw new Exception();
$move_char_array[$key] = $this->_chars[$val];
}
$new_string .= $move_char_array[$key];
}
if (bcmod($new_string, '97') != 1) {
throw new Exception();
}
} catch (Exception) {
$result->invalidate($tag, 'Invalid IBAN format');
}
}
}
<?php
require_once 'iban/index.php';
......@@ -6,14 +6,17 @@ use Exception;
class Integration
{
public static $fields = [];
private static $instances = [];
public function __construct()
{
add_action('init', function () {
$integration = self::get_instance();
$integration->register();
});
add_action('init', [$this, 'register']);
foreach (static::$fields as $field) {
$_field = $field::get_instance();
$_field->register();
}
}
protected function __clone()
......@@ -29,7 +32,7 @@ class Integration
{
$cls = static::class;
if (!isset(self::$instances[$cls])) {
self::$instances[$cls] = new static();
self::class::$instances[$cls] = new static();
}
return self::$instances[$cls];
......
File moved
......@@ -5,13 +5,17 @@ namespace WPCT_ERP_FORMS\Integrations;
use WPCT_ERP_FORMS\Integrations\Integration;
use Exception;
/* Custom fields */
require_once 'fields/iban/index.php';
require_once 'attachments.php';
require_once 'fields-population.php';
class GF extends Integration
{
public static $fields = [
IbanField::class
];
public function register()
{
add_action('gform_after_submission', function ($entry, $form) {
......
<?php
define('WPCT_ERP_FORMS_IBAN_FIELD_VERSION', '1.0');
add_action('gform_loaded', 'wpct_erp_forms_load_iban_field', 5);
function wpct_erp_forms_load_iban_field()
{
if (!method_exists('GFForms', 'include_addon_framework')) return;
require_once 'Addon.php';
require_once 'Field.php';
GFAddOn::register(\wpct_erp_FORMS\IBAN_Field\Addon::class);
}
......@@ -13,15 +13,15 @@ class WPCTIntegrationsRegistry
}
if (defined('WPCF7_VERSION')) {
require_once 'contactform7/index.php';
require_once 'wpcf7/index.php';
add_action('plugins_loaded', function () {
WPCTIntegrationsRegistry::$instances[] = new WPCF7();
WPCTIntegrationsRegistry::$instances['wpcf7'] = new WPCF7();
});
}
if (class_exists('GFForms')) {
require_once 'gravityforms/index.php';
require_once 'gf/index.php';
add_action('plugins_loaded', function () {
WPCTIntegrationsRegistry::$instances[] = new GF();
WPCTIntegrationsRegistry::$instances['gf'] = new GF();
});
}
......@@ -3,9 +3,13 @@
namespace WPCT_ERP_FORMS\Integrations;
use WPCT_ERP_FORMS\Integrations\Integration;
use WPCT_ERP_FORMS\Fields\WPCF7\Iban\Field as IbanField;
class WPCF7 extends Integration
{
public static $fields = [
IbanField::class
];
public function register()
{
......
<?php
/**
* Plugin Name: WPCT CRM Forms
* Plugin URI: https://git.coopdevs.org/coopdevs/website/wp/wp-plugins/wpct-crm-forms
* Description: Plugin to wire gravity forms submissions with Odoo CRM Leads module
* Plugin Name: Wpct ERP Forms
* Plugin URI: https://git.coopdevs.org/codeccoop/wp/wpct-erp-forms
* Description: Plugin to bridge WP forms submissions to a ERP backend
* Author: Còdec Cooperativa
* Author URI: https://www.codeccoop.org
* Text Domain: wpct-erp-forms
* Domain Path: /languages
* Version: 1.0.0
*
* @package WPCT_ERP_Forms
* @package wpct_erp_forms
*/
/* Settings */
require_once 'includes/options/index.php';
/* Webhooks */
require_once 'includes/attachments.php';
/* Fields population */
require_once 'includes/fields-population.php';
/* Integrations */
require_once 'includes/integrations/index.php';
/* Fields */
require_once 'includes/fields/index.php';
/* Dependencies */
add_filter('wpct_dependencies_check', function ($dependencies) {
// $dependencies['Gravity Forms'] = '<a href="https://www.gravityforms.com/">Gravity Forms</a>';
$dependencies['Wpct Odoo Connect'] = '<a href="https://git.coopdevs.org/coopdevs/website/wp/wp-plugins/wpct-odoo-connect">Wpct Odoo Connect</a>';
return $dependencies;
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment