Created
February 27, 2018 14:13
-
-
Save VasylKyryliuk/db729a578bc55ef494a78b84aea3da98 to your computer and use it in GitHub Desktop.
Phone mask plugin WordPress
This file contains hidden or 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
/* | |
jQuery Masked Input Plugin | |
Copyright (c) 2007 - 2015 Josh Bush (digitalbush.com) | |
Licensed under the MIT license (http://digitalbush.com/projects/masked-input-plugin/#license) | |
Version: 1.4.1 | |
change by author plugin contact form 7 mask field | |
*/ | |
!function(factory) { | |
"function" == typeof define && define.amd ? define([ "jquery" ], factory) : factory("object" == typeof exports ? require("jquery") : jQuery); | |
}(function($) { | |
var caretTimeoutId, ua = navigator.userAgent, iPhone = /iphone/i.test(ua), chrome = /chrome/i.test(ua), android = /android/i.test(ua); | |
$.mask = { | |
definitions: { | |
"_": "[0-9]", | |
a: "[A-Za-z]", | |
"*": "[A-Za-z0-9]" | |
}, | |
autoclear: !0, | |
dataName: "mask", | |
placeholder: "_" | |
}, $.fn.extend({ | |
caret: function(begin, end) { | |
var range; | |
if (0 !== this.length && !this.is(":hidden")) return "number" == typeof begin ? (end = "number" == typeof end ? end : begin, | |
this.each(function() { | |
this.setSelectionRange ? this.setSelectionRange(begin, end) : this.createTextRange && (range = this.createTextRange(), | |
range.collapse(!0), range.moveEnd("character", end), range.moveStart("character", begin), | |
range.select()); | |
})) : (this[0].setSelectionRange ? (begin = this[0].selectionStart, end = this[0].selectionEnd) : document.selection && document.selection.createRange && (range = document.selection.createRange(), | |
begin = 0 - range.duplicate().moveStart("character", -1e5), end = begin + range.text.length), | |
{ | |
begin: begin, | |
end: end | |
}); | |
}, | |
unmask: function() { | |
return this.trigger("unmask"); | |
}, | |
mask: function(mask, settings) { | |
var input, defs, tests, partialPosition, firstNonMaskPos, lastRequiredNonMaskPos, len, oldVal; | |
if (!mask && this.length > 0) { | |
input = $(this[0]); | |
var fn = input.data($.mask.dataName); | |
return fn ? fn() : void 0; | |
} | |
return settings = $.extend({ | |
autoclear: $.mask.autoclear, | |
placeholder: $.mask.placeholder, | |
completed: null | |
}, settings), defs = $.mask.definitions, tests = [], partialPosition = len = mask.length, | |
firstNonMaskPos = null, $.each(mask.split(""), function(i, c) { | |
"?" == c ? (len--, partialPosition = i) : defs[c] ? (tests.push(new RegExp(defs[c])), | |
null === firstNonMaskPos && (firstNonMaskPos = tests.length - 1), partialPosition > i && (lastRequiredNonMaskPos = tests.length - 1)) : tests.push(null); | |
}), this.trigger("unmask").each(function() { | |
function tryFireCompleted() { | |
if (settings.completed) { | |
for (var i = firstNonMaskPos; lastRequiredNonMaskPos >= i; i++) if (tests[i] && buffer[i] === getPlaceholder(i)) return; | |
settings.completed.call(input); | |
} | |
} | |
function getPlaceholder(i) { | |
return settings.placeholder.charAt(i < settings.placeholder.length ? i : 0); | |
} | |
function seekNext(pos) { | |
for (;++pos < len && !tests[pos]; ) ; | |
return pos; | |
} | |
function seekPrev(pos) { | |
for (;--pos >= 0 && !tests[pos]; ) ; | |
return pos; | |
} | |
function shiftL(begin, end) { | |
var i, j; | |
if (!(0 > begin)) { | |
for (i = begin, j = seekNext(end); len > i; i++) if (tests[i]) { | |
if (!(len > j && tests[i].test(buffer[j]))) break; | |
buffer[i] = buffer[j], buffer[j] = getPlaceholder(j), j = seekNext(j); | |
} | |
writeBuffer(), input.caret(Math.max(firstNonMaskPos, begin)); | |
} | |
} | |
function shiftR(pos) { | |
var i, c, j, t; | |
for (i = pos, c = getPlaceholder(pos); len > i; i++) if (tests[i]) { | |
if (j = seekNext(i), t = buffer[i], buffer[i] = c, !(len > j && tests[j].test(t))) break; | |
c = t; | |
} | |
} | |
function androidInputEvent() { | |
var curVal = input.val(), pos = input.caret(); | |
if (oldVal && oldVal.length && oldVal.length > curVal.length) { | |
for (checkVal(!0); pos.begin > 0 && !tests[pos.begin - 1]; ) pos.begin--; | |
if (0 === pos.begin) for (;pos.begin < firstNonMaskPos && !tests[pos.begin]; ) pos.begin++; | |
input.caret(pos.begin, pos.begin); | |
} else { | |
for (checkVal(!0); pos.begin < len && !tests[pos.begin]; ) pos.begin++; | |
input.caret(pos.begin, pos.begin); | |
} | |
tryFireCompleted(); | |
} | |
function blurEvent() { | |
checkVal(), input.val() != focusText && input.change(); | |
} | |
function keydownEvent(e) { | |
if (!input.prop("readonly")) { | |
var pos, begin, end, k = e.which || e.keyCode; | |
oldVal = input.val(), 8 === k || 46 === k || iPhone && 127 === k ? (pos = input.caret(), | |
begin = pos.begin, end = pos.end, end - begin === 0 && (begin = 46 !== k ? seekPrev(begin) : end = seekNext(begin - 1), | |
end = 46 === k ? seekNext(end) : end), clearBuffer(begin, end), shiftL(begin, end - 1), | |
e.preventDefault()) : 13 === k ? blurEvent.call(this, e) : 27 === k && (input.val(focusText), | |
input.caret(0, checkVal()), e.preventDefault()); | |
} | |
} | |
function keypressEvent(e) { | |
if (!input.prop("readonly")) { | |
var p, c, next, k = e.which || e.keyCode, pos = input.caret(); | |
if (!(e.ctrlKey || e.altKey || e.metaKey || 32 > k) && k && 13 !== k) { | |
if (pos.end - pos.begin !== 0 && (clearBuffer(pos.begin, pos.end), shiftL(pos.begin, pos.end - 1)), | |
p = seekNext(pos.begin - 1), len > p && (c = String.fromCharCode(k), tests[p].test(c))) { | |
if (shiftR(p), buffer[p] = c, writeBuffer(), next = seekNext(p), android) { | |
var proxy = function() { | |
$.proxy($.fn.caret, input, next)(); | |
}; | |
setTimeout(proxy, 0); | |
} else input.caret(next); | |
pos.begin <= lastRequiredNonMaskPos && tryFireCompleted(); | |
} | |
e.preventDefault(); | |
} | |
} | |
} | |
function clearBuffer(start, end) { | |
var i; | |
for (i = start; end > i && len > i; i++) tests[i] && (buffer[i] = getPlaceholder(i)); | |
} | |
function writeBuffer() { | |
input.val(buffer.join("")); | |
} | |
function checkVal(allow) { | |
var i, c, pos, test = input.val(), lastMatch = -1; | |
for (i = 0, pos = 0; len > i; i++) if (tests[i]) { | |
for (buffer[i] = getPlaceholder(i); pos++ < test.length; ) if (c = test.charAt(pos - 1), | |
tests[i].test(c)) { | |
buffer[i] = c, lastMatch = i; | |
break; | |
} | |
if (pos > test.length) { | |
clearBuffer(i + 1, len); | |
break; | |
} | |
} else buffer[i] === test.charAt(pos) && pos++, partialPosition > i && (lastMatch = i); | |
return allow ? writeBuffer() : partialPosition > lastMatch + 1 ? settings.autoclear || buffer.join("") === defaultBuffer ? (input.val() && input.val(""), | |
clearBuffer(0, len)) : writeBuffer() : (writeBuffer(), input.val(input.val().substring(0, lastMatch + 1))), | |
partialPosition ? i : firstNonMaskPos; | |
} | |
var input = $(this), buffer = $.map(mask.split(""), function(c, i) { | |
return "?" != c ? defs[c] ? getPlaceholder(i) : c : void 0; | |
}), defaultBuffer = buffer.join(""), focusText = input.val(); | |
input.data($.mask.dataName, function() { | |
return $.map(buffer, function(c, i) { | |
return tests[i] && c != getPlaceholder(i) ? c : null; | |
}).join(""); | |
}), input.one("unmask", function() { | |
input.off(".mask").removeData($.mask.dataName); | |
}).on("focus.mask", function() { | |
if (!input.prop("readonly")) { | |
clearTimeout(caretTimeoutId); | |
var pos; | |
focusText = input.val(), pos = checkVal(), caretTimeoutId = setTimeout(function() { | |
input.get(0) === document.activeElement && (writeBuffer(), pos == mask.replace("?", "").length ? input.caret(0, pos) : input.caret(pos)); | |
}, 10); | |
} | |
}).on("blur.mask", blurEvent).on("keydown.mask", keydownEvent).on("keypress.mask", keypressEvent).on("input.mask paste.mask", function() { | |
input.prop("readonly") || setTimeout(function() { | |
var pos = checkVal(!0); | |
input.caret(pos), tryFireCompleted(); | |
}, 0); | |
}), chrome && android && input.off("input.mask").on("input.mask", androidInputEvent), | |
checkVal(); | |
}); | |
} | |
}); | |
// contact form 7 mask field | |
var $mask_fields = $('.wpcf7-mask'); | |
if ( $mask_fields.length > 0 ) { | |
$mask_fields.each(function(){ | |
var $this = $(this) | |
$this.mask( $this.data('mask') ); | |
}); | |
} | |
}); |
This file contains hidden or 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
!function(factory){"function"==typeof define&&define.amd?define(["jquery"],factory):factory("object"==typeof exports?require("jquery"):jQuery);}(function($){var caretTimeoutId,ua=navigator.userAgent,iPhone=/iphone/i.test(ua),chrome=/chrome/i.test(ua),android=/android/i.test(ua);$.mask={definitions:{"_":"[0-9]",a:"[A-Za-z]","*":"[A-Za-z0-9]"},autoclear:!0,dataName:"mask",placeholder:"_"},$.fn.extend({caret:function(begin,end){var range;if(0!==this.length&&!this.is(":hidden"))return"number"==typeof begin?(end="number"==typeof end?end:begin,this.each(function(){this.setSelectionRange?this.setSelectionRange(begin,end):this.createTextRange&&(range=this.createTextRange(),range.collapse(!0),range.moveEnd("character",end),range.moveStart("character",begin),range.select());})):(this[0].setSelectionRange?(begin=this[0].selectionStart,end=this[0].selectionEnd):document.selection&&document.selection.createRange&&(range=document.selection.createRange(),begin=0-range.duplicate().moveStart("character",-1e5),end=begin+range.text.length),{begin:begin,end:end});},unmask:function(){return this.trigger("unmask");},mask:function(mask,settings){var input,defs,tests,partialPosition,firstNonMaskPos,lastRequiredNonMaskPos,len,oldVal;if(!mask&&this.length>0){input=$(this[0]);var fn=input.data($.mask.dataName);return fn?fn():void 0;}return settings=$.extend({autoclear:$.mask.autoclear,placeholder:$.mask.placeholder,completed:null},settings),defs=$.mask.definitions,tests=[],partialPosition=len=mask.length,firstNonMaskPos=null,$.each(mask.split(""),function(i,c){"?"==c?(len--,partialPosition=i):defs[c]?(tests.push(new RegExp(defs[c])),null===firstNonMaskPos&&(firstNonMaskPos=tests.length-1),partialPosition>i&&(lastRequiredNonMaskPos=tests.length-1)):tests.push(null);}),this.trigger("unmask").each(function(){function tryFireCompleted(){if(settings.completed){for(var i=firstNonMaskPos;lastRequiredNonMaskPos>=i;i++)if(tests[i]&&buffer[i]===getPlaceholder(i))return;settings.completed.call(input);}}function getPlaceholder(i){return settings.placeholder.charAt(i<settings.placeholder.length?i:0);}function seekNext(pos){for(;++pos<len&&!tests[pos];);return pos;}function seekPrev(pos){for(;--pos>=0&&!tests[pos];);return pos;}function shiftL(begin,end){var i,j;if(!(0>begin)){for(i=begin,j=seekNext(end);len>i;i++)if(tests[i]){if(!(len>j&&tests[i].test(buffer[j])))break;buffer[i]=buffer[j],buffer[j]=getPlaceholder(j),j=seekNext(j);}writeBuffer(),input.caret(Math.max(firstNonMaskPos,begin));}}function shiftR(pos){var i,c,j,t;for(i=pos,c=getPlaceholder(pos);len>i;i++)if(tests[i]){if(j=seekNext(i),t=buffer[i],buffer[i]=c,!(len>j&&tests[j].test(t)))break;c=t;}}function androidInputEvent(){var curVal=input.val(),pos=input.caret();if(oldVal&&oldVal.length&&oldVal.length>curVal.length){for(checkVal(!0);pos.begin>0&&!tests[pos.begin-1];)pos.begin--;if(0===pos.begin)for(;pos.begin<firstNonMaskPos&&!tests[pos.begin];)pos.begin++;input.caret(pos.begin,pos.begin);}else{for(checkVal(!0);pos.begin<len&&!tests[pos.begin];)pos.begin++;input.caret(pos.begin,pos.begin);}tryFireCompleted();}function blurEvent(){checkVal(),input.val()!=focusText&&input.change();}function keydownEvent(e){if(!input.prop("readonly")){var pos,begin,end,k=e.which||e.keyCode;oldVal=input.val(),8===k||46===k||iPhone&&127===k?(pos=input.caret(),begin=pos.begin,end=pos.end,end-begin===0&&(begin=46!==k?seekPrev(begin):end=seekNext(begin-1),end=46===k?seekNext(end):end),clearBuffer(begin,end),shiftL(begin,end-1),e.preventDefault()):13===k?blurEvent.call(this,e):27===k&&(input.val(focusText),input.caret(0,checkVal()),e.preventDefault());}}function keypressEvent(e){if(!input.prop("readonly")){var p,c,next,k=e.which||e.keyCode,pos=input.caret();if(!(e.ctrlKey||e.altKey||e.metaKey||32>k)&&k&&13!==k){if(pos.end-pos.begin!==0&&(clearBuffer(pos.begin,pos.end),shiftL(pos.begin,pos.end-1)),p=seekNext(pos.begin-1),len>p&&(c=String.fromCharCode(k),tests[p].test(c))){if(shiftR(p),buffer[p]=c,writeBuffer(),next=seekNext(p),android){var proxy=function(){$.proxy($.fn.caret,input,next)();};setTimeout(proxy,0);}else input.caret(next);pos.begin<=lastRequiredNonMaskPos&&tryFireCompleted();}e.preventDefault();}}}function clearBuffer(start,end){var i;for(i=start;end>i&&len>i;i++)tests[i]&&(buffer[i]=getPlaceholder(i));}function writeBuffer(){input.val(buffer.join(""));}function checkVal(allow){var i,c,pos,test=input.val(),lastMatch=-1;for(i=0,pos=0;len>i;i++)if(tests[i]){for(buffer[i]=getPlaceholder(i);pos++<test.length;)if(c=test.charAt(pos-1),tests[i].test(c)){buffer[i]=c,lastMatch=i;break;}if(pos>test.length){clearBuffer(i+1,len);break;}}else buffer[i]===test.charAt(pos)&&pos++,partialPosition>i&&(lastMatch=i);return allow?writeBuffer():partialPosition>lastMatch+1?settings.autoclear||buffer.join("")===defaultBuffer?(input.val()&&input.val(""),clearBuffer(0,len)):writeBuffer():(writeBuffer(),input.val(input.val().substring(0,lastMatch+1))),partialPosition?i:firstNonMaskPos;}var input=$(this),buffer=$.map(mask.split(""),function(c,i){return"?"!=c?defs[c]?getPlaceholder(i):c:void 0;}),defaultBuffer=buffer.join(""),focusText=input.val();input.data($.mask.dataName,function(){return $.map(buffer,function(c,i){return tests[i]&&c!=getPlaceholder(i)?c:null;}).join("");}),input.one("unmask",function(){input.off(".mask").removeData($.mask.dataName);}).on("focus.mask",function(){if(!input.prop("readonly")){clearTimeout(caretTimeoutId);var pos;focusText=input.val(),pos=checkVal(),caretTimeoutId=setTimeout(function(){input.get(0)===document.activeElement&&(writeBuffer(),pos==mask.replace("?","").length?input.caret(0,pos):input.caret(pos));},10);}}).on("blur.mask",blurEvent).on("keydown.mask",keydownEvent).on("keypress.mask",keypressEvent).on("input.mask paste.mask",function(){input.prop("readonly")||setTimeout(function(){var pos=checkVal(!0);input.caret(pos),tryFireCompleted();},0);}),chrome&&android&&input.off("input.mask").on("input.mask",androidInputEvent),checkVal();});}});var $mask_fields=$('.wpcf7-mask');if($mask_fields.length>0){$mask_fields.each(function(){var $this=$(this) | |
$this.mask($this.data('mask'));});}}); |
This file contains hidden or 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
?? ? ? ? ? ! | |
M / | |
} a ? ? | |
? i , | |
? ? ? ( ? D Z ? ? ? ? ? ? | |
? ? ! ? % 2 Class attribute Contact Form 7 - Phone mask field Enter the mask for this field. <br /><code>Example: +1 (___) ___-__-__</code> Field type Generate a form-tag for a single-line plain text input field in which you can set the input mask. Id attribute Insert Tag Mask Name Required field Ruslan Heorhiiev This plugin adds a new field in which you can set the phone number entry mask or other to Contact Form 7. mask field PO-Revision-Date: 2017-11-22 19:29:53+0000 | |
MIME-Version: 1.0 | |
Content-Type: text/plain; charset=UTF-8 | |
Content-Transfer-Encoding: 8bit | |
Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2); | |
X-Generator: GlotPress/2.4.0-alpha | |
Language: ru | |
Project-Id-Version: Plugins - Contact Form 7 – Phone mask field - Development (trunk) | |
атрибут класса Contact Form 7 - Маска телефонного номера Введите маску вводу. <br /><code>Пример: +7 (921) ___-__-__</code> Тип поля Генерируйте тег формы для однострочного поля ввода текста в котором можно установить маску ввода. атрибут Id Вставить тег Маска Имя Обязательное поле Руслан Георгиев Плагин добавляет новое текстовое поле для которого можно установить маску ввода, например маску ввода телефонного номера. Плагин расширяет Contact Form 7. текст с маской ввода |
This file contains hidden or 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
# Translation of Plugins - Contact Form 7 – Phone mask field - Development (trunk) in Russian | |
# This file is distributed under the same license as the Plugins - Contact Form 7 – Phone mask field - Development (trunk) package. | |
msgid "" | |
msgstr "" | |
"PO-Revision-Date: 2017-11-22 19:29:53+0000\n" | |
"MIME-Version: 1.0\n" | |
"Content-Type: text/plain; charset=UTF-8\n" | |
"Content-Transfer-Encoding: 8bit\n" | |
"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n" | |
"X-Generator: GlotPress/2.4.0-alpha\n" | |
"Language: ru\n" | |
"Project-Id-Version: Plugins - Contact Form 7 – Phone mask field - Development (trunk)\n" | |
#. Author of the plugin/theme | |
msgid "Ruslan Heorhiiev" | |
msgstr "Руслан Георгиев" | |
#. Description of the plugin/theme | |
msgid "This plugin adds a new field in which you can set the phone number entry mask or other to Contact Form 7." | |
msgstr "Плагин добавляет новое текстовое поле для которого можно установить маску ввода, например маску ввода телефонного номера. Плагин расширяет Contact Form 7." | |
#. Plugin Name of the plugin/theme | |
msgid "Contact Form 7 - Phone mask field" | |
msgstr "Contact Form 7 - Маска телефонного номера" | |
#: cf7-phone-mask-field.php:238 | |
msgid "Insert Tag" | |
msgstr "Вставить тег" | |
#: cf7-phone-mask-field.php:224 | |
msgid "Class attribute" | |
msgstr "атрибут класса" | |
#: cf7-phone-mask-field.php:219 | |
msgid "Id attribute" | |
msgstr "атрибут Id" | |
#: cf7-phone-mask-field.php:215 | |
msgid "Enter the mask for this field. <br /><code>Example: +1 (___) ___-__-__</code>" | |
msgstr "Введите маску вводу. <br /><code>Пример: +7 (921) ___-__-__</code>" | |
#: cf7-phone-mask-field.php:213 | |
msgid "Mask" | |
msgstr "Маска" | |
#: cf7-phone-mask-field.php:208 | |
msgid "Name" | |
msgstr "Имя" | |
#: cf7-phone-mask-field.php:202 | |
msgid "Required field" | |
msgstr "Обязательное поле" | |
#: cf7-phone-mask-field.php:198 cf7-phone-mask-field.php:201 | |
msgid "Field type" | |
msgstr "Тип поля" | |
#: cf7-phone-mask-field.php:193 | |
msgid "Generate a form-tag for a single-line plain text input field in which you can set the input mask." | |
msgstr "Генерируйте тег формы для однострочного поля ввода текста в котором можно установить маску ввода." | |
#: cf7-phone-mask-field.php:174 | |
msgid "mask field" | |
msgstr "текст с маской ввода" |
This file contains hidden or 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: Contact Form 7 - Phone mask field | |
Description: This plugin adds a new field in which you can set the phone number entry mask or other to Contact Form 7. | |
Version: 1.0 | |
Author: Ruslan Heorhiiev | |
Text Domain: cf7-phone-mask-field | |
Domain Path: /assets/languages/ | |
Copyright © 2017 Ruslan Heorhiiev | |
*/ | |
if ( ! ABSPATH ) exit; | |
/** | |
* Функция инициализации плагина | |
* Function init plugin | |
**/ | |
function wpcf7mf_init(){ | |
add_action( 'wpcf7_init', 'wpcf7mf_add_shortcode_mask' ); | |
add_action( 'wp_enqueue_scripts', 'wpcf7mf_do_enqueue_scripts' ); | |
add_filter( 'wpcf7_validate_mask*', 'wpcf7mf_mask_validation_filter', 10, 2 ); | |
// загрузка language файла & load language file. | |
load_plugin_textdomain( 'inwidget', false, dirname( plugin_dir_path( __FILE__ ) ) . '/assets/languages/' ); | |
} | |
add_action( 'plugins_loaded', 'wpcf7mf_init' , 20 ); | |
/** | |
* Функция подключения JS | |
* Function enqueu script | |
* @version 1.0 | |
**/ | |
function wpcf7mf_do_enqueue_scripts() { | |
wp_enqueue_script( 'wpcf7mf-mask', plugin_dir_url( __FILE__ ) . 'assets/js/jquery.maskedinput.min.js', array('jquery'), '1.4', true ); | |
} | |
add_action( 'wp_enqueue_scripts', 'wpcf7mf_do_enqueue_scripts' ); | |
/** | |
* Функция добавления поля маски в wpcf7 | |
* Function add mask field in wpcf7 | |
* @version 1.0 | |
**/ | |
function wpcf7mf_add_shortcode_mask() { | |
wpcf7_add_form_tag( | |
array( 'mask' , 'mask*' ), | |
'wpcf7mf_mask_shortcode_handler', true ); | |
} | |
/** | |
* Функция добавления шорткодов с участием маски | |
* Function add shortcodes with mask | |
* @version 1.0 | |
**/ | |
function wpcf7mf_mask_shortcode_handler( $tag ) { | |
$tag = new WPCF7_FormTag( $tag ); | |
if ( empty( $tag->name ) ) | |
return ''; | |
$validation_error = wpcf7_get_validation_error( $tag->name ); | |
$class = wpcf7_form_controls_class( $tag->type, 'wpcf7mf-mask' ); | |
if ( $validation_error ) | |
$class .= ' wpcf7-not-valid'; | |
$atts = array(); | |
$atts['size'] = $tag->get_size_option( '40' ); | |
$atts['maxlength'] = $tag->get_maxlength_option(); | |
$atts['minlength'] = $tag->get_minlength_option(); | |
if ( $atts['maxlength'] && $atts['minlength'] && $atts['maxlength'] < $atts['minlength'] ) { | |
unset( $atts['maxlength'], $atts['minlength'] ); | |
} | |
$atts['class'] = $tag->get_class_option( $class ); | |
$atts['id'] = $tag->get_id_option(); | |
$atts['tabindex'] = $tag->get_option( 'tabindex', 'int', true ); | |
if ( $tag->has_option( 'readonly' ) ) | |
$atts['readonly'] = 'readonly'; | |
if ( $tag->is_required() ) | |
$atts['aria-required'] = 'true'; | |
$atts['aria-invalid'] = $validation_error ? 'true' : 'false'; | |
$value = (string) reset( $tag->values ); | |
$value = $tag->get_default_option( $value ); | |
$atts['placeholder'] = $value; | |
$atts['value'] = ''; | |
$atts['data-mask'] = $value; | |
$atts['type'] = 'text'; | |
$atts['name'] = $tag->name; | |
$atts = wpcf7_format_atts( $atts ); | |
$html = sprintf( | |
'<span class="wpcf7-form-control-wrap %1$s"><input %2$s />%3$s</span>', | |
sanitize_html_class( $tag->name ), $atts, $validation_error ); | |
return $html; | |
} | |
/** | |
* Функция проверка поля маски | |
* Function check mask field | |
* @version 1.0 | |
**/ | |
function wpcf7mf_mask_validation_filter( $result, $tag ) { | |
$tag = new WPCF7_FormTag( $tag ); | |
$name = $tag->name; | |
$value = isset( $_POST[$name] ) | |
? trim( wp_unslash( strtr( (string) $_POST[$name], "\n", " " ) ) ) | |
: ''; | |
if ( 'mask' == $tag->basetype ) { | |
if ( $tag->is_required() && '' == $value ) { | |
$result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) ); | |
} | |
} | |
if ( ! empty( $value ) ) { | |
$maxlength = $tag->get_maxlength_option(); | |
$minlength = $tag->get_minlength_option(); | |
if ( $maxlength && $minlength && $maxlength < $minlength ) { | |
$maxlength = $minlength = null; | |
} | |
$code_units = wpcf7_count_code_units( $value ); | |
if ( false !== $code_units ) { | |
if ( $maxlength && $maxlength < $code_units ) { | |
$result->invalidate( $tag, wpcf7_get_message( 'invalid_too_long' ) ); | |
} elseif ( $minlength && $code_units < $minlength ) { | |
$result->invalidate( $tag, wpcf7_get_message( 'invalid_too_short' ) ); | |
} | |
} | |
} | |
return $result; | |
} | |
if ( is_admin() ) { | |
add_action( 'wpcf7_admin_init' , 'wpcf7mf_add_tag_generator_field' , 100 ); | |
} | |
/** | |
* Функция вызова генератора тегов WPCF7 | |
* Function cell Tag GeneratorWPCF7 | |
* @version 1.0 | |
**/ | |
function wpcf7mf_add_tag_generator_field() { | |
if ( ! class_exists( 'WPCF7_TagGenerator' ) ) return; | |
$tag_generator = WPCF7_TagGenerator::get_instance(); | |
$tag_generator->add( | |
'mask', | |
__( 'mask field', 'contact-form-7-mask-field' ), | |
'wpcf7mf_tag_generator_field' | |
); | |
} | |
/** | |
* Функция генерирования поля | |
* Function generating new field | |
* @version 1.0 | |
**/ | |
function wpcf7mf_tag_generator_field( $contact_form , $args = '' ){ | |
$args = wp_parse_args( $args, array() ); | |
$type = $args['id']; | |
?> | |
<div class="control-box"> | |
<fieldset> | |
<legend><?php echo esc_html( __( 'Generate a form-tag for a single-line plain text input field in which you can set the input mask.', 'contact-form-7-mask-field' ) ); ?></legend> | |
<table class="form-table"> | |
<tbody> | |
<tr> | |
<th scope="row"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></th> | |
<td> | |
<fieldset> | |
<legend class="screen-reader-text"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></legend> | |
<label><input type="checkbox" name="required" /> <?php echo esc_html( __( 'Required field', 'contact-form-7' ) ); ?></label> | |
</fieldset> | |
</td> | |
</tr> | |
<tr> | |
<th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th> | |
<td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td> | |
</tr> | |
<!-- msk field --> | |
<tr> | |
<th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-values' ); ?>"><?php echo esc_html( __( 'Mask', 'contact-form-7-mask-field' ) ); ?></label></th> | |
<td><input type="text" name="values" class="oneline" id="<?php echo esc_attr( $args['content'] . '-values' ); ?>" /><br /> | |
<?php _e( 'Enter the mask for this field. <br /><code>Example: +1 (___) ___-__-__</code>', 'contact-form-7-mask-field' ); ?></td> | |
</tr> | |
<tr> | |
<th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-id' ); ?>"><?php echo esc_html( __( 'Id attribute', 'contact-form-7' ) ); ?></label></th> | |
<td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-id' ); ?>" /></td> | |
</tr> | |
<tr> | |
<th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-class' ); ?>"><?php echo esc_html( __( 'Class attribute', 'contact-form-7' ) ); ?></label></th> | |
<td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-class' ); ?>" /></td> | |
</tr> | |
</tbody> | |
</table> | |
</fieldset> | |
</div> | |
<div class="insert-box"> | |
<input type="text" name="<?php echo $type; ?>" class="tag code" readonly="readonly" onfocus="this.select()" /> | |
<div class="submitbox"> | |
<input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" /> | |
</div> | |
<br class="clear" /> | |
</div> | |
<?php | |
} |
This file contains hidden or 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
=== Contact Form 7 - Phone mask field === | |
Contributors: heorhiiev | |
Tags: Contact Form 7, Contact Form 7 phone, mask, mask field, phone field, telephone field, telephone, custom mask, custom mask field, cf7, contact form 7 mask field, contact form 7 phone mask, contact form 7 phone field, маска ввода, маска телефонного номера | |
Requires at least: 4.0 | |
Tested up to: 4.9 | |
Stable tag: 1.0 | |
License: GPLv2 or later | |
License URI: http://www.gnu.org/licenses/gpl-2.0.html | |
This plugin adds a new field in which you can set the phone number mask or other to Contact Form 7. | |
== Description == | |
This plugin adds a new field in which you can set the phone number mask or other to Contact Form 7. | |
Please notice that Contact Form 7 must be installed and active. | |
A new field "mask field" will be added to the Contact Form 7 panel buttons. | |
== Installation == | |
Just install from your WordPress "Plugins > Add New" screen and all will be well. Manual installation is very straightforward as well: | |
1. Upload the `cf7-phone-mask-field` folder to the `/wp-content/plugins/` directory | |
1. Activate the plugin through the 'Plugins' menu in WordPress | |
1. That's it! | |
== Screenshots == | |
1. New field in Contact Form 7 | |
== Upgrade Notice == | |
== Changelog == | |
= 1.0 = | |
* First released version. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment