| 1 |
<?php |
| 2 |
|
| 3 |
namespace MailPoet\Form\Block; |
| 4 |
|
| 5 |
use MailPoet\Form\Util\FieldNameObfuscator; |
| 6 |
use MailPoet\Models\ModelValidator; |
| 7 |
|
| 8 |
abstract class Base { |
| 9 |
protected static function getInputValidation($block, $extra_rules = array()) { |
| 10 |
$rules = array(); |
| 11 |
|
| 12 |
if($block['id'] === 'email') { |
| 13 |
$rules['required'] = true; |
| 14 |
$rules['minlength'] = ModelValidator::EMAIL_MIN_LENGTH; |
| 15 |
$rules['maxlength'] = ModelValidator::EMAIL_MAX_LENGTH; |
| 16 |
$rules['error-message'] = __('Please specify a valid email address.', 'mailpoet'); |
| 17 |
} |
| 18 |
|
| 19 |
if($block['id'] === 'segments') { |
| 20 |
$rules['required'] = true; |
| 21 |
$rules['mincheck'] = 1; |
| 22 |
$rules['group'] = $block['id']; |
| 23 |
$rules['errors-container'] = '.mailpoet_error_'.$block['id']; |
| 24 |
$rules['required-message'] = __('Please select a list', 'mailpoet'); |
| 25 |
} |
| 26 |
|
| 27 |
if(!empty($block['params']['required'])) { |
| 28 |
$rules['required'] = true; |
| 29 |
$rules['required-message'] = __('This field is required.', 'mailpoet'); |
| 30 |
} |
| 31 |
|
| 32 |
if(!empty($block['params']['validate'])) { |
| 33 |
if($block['params']['validate'] === 'phone') { |
| 34 |
$rules['pattern'] = "^[\d\+\-\.\(\)\/\s]*$"; |
| 35 |
$rules['error-message'] = __('Please specify a valid phone number', 'mailpoet'); |
| 36 |
} else { |
| 37 |
$rules['type'] = $block['params']['validate']; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
if(in_array($block['type'], array('radio', 'checkbox'))) { |
| 42 |
$rules['group'] = 'custom_field_'.$block['id']; |
| 43 |
$rules['errors-container'] = '.mailpoet_error_'.$block['id']; |
| 44 |
$rules['required-message'] = __('Please select at least one option', 'mailpoet'); |
| 45 |
} |
| 46 |
|
| 47 |
if($block['type'] === 'date') { |
| 48 |
$rules['group'] = 'custom_field_'.$block['id']; |
| 49 |
$rules['errors-container'] = '.mailpoet_error_'.$block['id']; |
| 50 |
} |
| 51 |
|
| 52 |
$validation = array(); |
| 53 |
|
| 54 |
$rules = array_merge($rules, $extra_rules); |
| 55 |
|
| 56 |
if(!empty($rules)) { |
| 57 |
$rules = array_unique($rules); |
| 58 |
foreach($rules as $rule => $value) { |
| 59 |
if(is_bool($value)) { |
| 60 |
$value = ($value) ? 'true' : 'false'; |
| 61 |
} |
| 62 |
$validation[] = 'data-parsley-'.$rule.'="'.$value.'"'; |
| 63 |
} |
| 64 |
} |
| 65 |
return join(' ', $validation); |
| 66 |
} |
| 67 |
|
| 68 |
protected static function renderLabel($block) { |
| 69 |
$html = ''; |
| 70 |
if( |
| 71 |
isset($block['params']['label_within']) |
| 72 |
&& $block['params']['label_within'] |
| 73 |
) { |
| 74 |
return $html; |
| 75 |
} |
| 76 |
if(isset($block['params']['label']) |
| 77 |
&& strlen(trim($block['params']['label'])) > 0) { |
| 78 |
$html .= '<label class="mailpoet_'.$block['type'].'_label">'; |
| 79 |
$html .= $block['params']['label']; |
| 80 |
|
| 81 |
if(isset($block['params']['required']) && $block['params']['required']) { |
| 82 |
$html .= ' <span class="mailpoet_required">*</span>'; |
| 83 |
} |
| 84 |
|
| 85 |
$html .= '</label>'; |
| 86 |
} |
| 87 |
return $html; |
| 88 |
} |
| 89 |
|
| 90 |
protected static function renderInputPlaceholder($block) { |
| 91 |
$html = ''; |
| 92 |
// if the label is displayed as a placeholder, |
| 93 |
if( |
| 94 |
isset($block['params']['label_within']) |
| 95 |
&& $block['params']['label_within'] |
| 96 |
) { |
| 97 |
// display only label |
| 98 |
$html .= ' placeholder="'; |
| 99 |
$html .= static::getFieldLabel($block); |
| 100 |
// add an asterisk if it's a required field |
| 101 |
if(isset($block['params']['required']) && $block['params']['required']) { |
| 102 |
$html .= ' *'; |
| 103 |
} |
| 104 |
$html .= '" '; |
| 105 |
} |
| 106 |
return $html; |
| 107 |
} |
| 108 |
|
| 109 |
// return field name depending on block data |
| 110 |
protected static function getFieldName($block = array()) { |
| 111 |
if((int)$block['id'] > 0) { |
| 112 |
return 'cf_'.$block['id']; |
| 113 |
} else { |
| 114 |
$obfuscator = new FieldNameObfuscator(); |
| 115 |
return $obfuscator->obfuscate($block['id']);//obfuscate field name for spambots |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
protected static function getFieldLabel($block = array()) { |
| 120 |
return (isset($block['params']['label']) |
| 121 |
&& strlen(trim($block['params']['label'])) > 0) |
| 122 |
? trim($block['params']['label']) : ''; |
| 123 |
} |
| 124 |
|
| 125 |
protected static function getFieldValue($block = array()) { |
| 126 |
return (isset($block['params']['value']) |
| 127 |
&& strlen(trim($block['params']['value'])) > 0) |
| 128 |
? esc_attr(trim($block['params']['value'])) : ''; |
| 129 |
} |
| 130 |
|
| 131 |
protected static function getInputModifiers($block = array()) { |
| 132 |
$modifiers = array(); |
| 133 |
|
| 134 |
if(isset($block['params']['readonly']) && $block['params']['readonly']) { |
| 135 |
$modifiers[] = 'readonly'; |
| 136 |
} |
| 137 |
|
| 138 |
if(isset($block['params']['disabled']) && $block['params']['disabled']) { |
| 139 |
$modifiers[] = 'disabled'; |
| 140 |
} |
| 141 |
return join(' ', $modifiers); |
| 142 |
} |
| 143 |
} |