Field.php
201 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace WPDM\Form; |
| 5 | |
| 6 | |
| 7 | use WPDM\__\__; |
| 8 | |
| 9 | class Field |
| 10 | { |
| 11 | |
| 12 | static function heading($attrs) |
| 13 | { |
| 14 | $_attrs = ""; |
| 15 | $text = $attrs['text']; |
| 16 | unset($attrs['text']); |
| 17 | foreach ($attrs as $key => $value) { |
| 18 | $_attrs .= "{$key}='{$value}' "; |
| 19 | } |
| 20 | return "<div class=''>{$text}</div>"; |
| 21 | } |
| 22 | |
| 23 | static function hidden($attrs) |
| 24 | { |
| 25 | $_attrs = ""; |
| 26 | foreach ($attrs as $key => $value) { |
| 27 | $_attrs .= "{$key}='{$value}' "; |
| 28 | } |
| 29 | $text = "<input type='hidden' $_attrs />"; |
| 30 | return $text; |
| 31 | } |
| 32 | |
| 33 | static function text($attrs) |
| 34 | { |
| 35 | $_attrs = ""; |
| 36 | $attrs['class'] = isset($attrs['class']) ? "form-control " . $attrs['class'] : "form-control"; |
| 37 | foreach ($attrs as $key => $value) { |
| 38 | $_attrs .= "{$key}='{$value}' "; |
| 39 | } |
| 40 | $text = "<input type='text' $_attrs />"; |
| 41 | return $text; |
| 42 | } |
| 43 | |
| 44 | static function textarea($attrs, $value = '') |
| 45 | { |
| 46 | $_attrs = ""; |
| 47 | $attrs['class'] = isset($attrs['class']) ? "form-control " . $attrs['class'] : "form-control"; |
| 48 | foreach ($attrs as $key => $_value) { |
| 49 | $_attrs .= "{$key}='{$_value}' "; |
| 50 | } |
| 51 | $value = stripslashes_deep($value); |
| 52 | $text = "<textarea $_attrs>{$value}</textarea>"; |
| 53 | return $text; |
| 54 | } |
| 55 | |
| 56 | static function number($attrs) |
| 57 | { |
| 58 | $_attrs = ""; |
| 59 | $attrs['class'] = isset($attrs['class']) ? "form-control " . $attrs['class'] : "form-control"; |
| 60 | foreach ($attrs as $key => $value) { |
| 61 | $_attrs .= "{$key}='{$value}' "; |
| 62 | } |
| 63 | $text = "<input type='number' $_attrs />"; |
| 64 | return $text; |
| 65 | } |
| 66 | |
| 67 | static function email($attrs) |
| 68 | { |
| 69 | $_attrs = ""; |
| 70 | $attrs['class'] = isset($attrs['class']) ? "form-control " . $attrs['class'] : "form-control"; |
| 71 | foreach ($attrs as $key => $value) { |
| 72 | $_attrs .= "{$key}='{$value}' "; |
| 73 | } |
| 74 | return "<input type='email' $_attrs />"; |
| 75 | } |
| 76 | |
| 77 | static function password($attrs) |
| 78 | { |
| 79 | $_attrs = ""; |
| 80 | $attrs['class'] = isset($attrs['class']) ? "form-control " . $attrs['class'] : "form-control"; |
| 81 | foreach ($attrs as $key => $value) { |
| 82 | $_attrs .= "{$key}='{$value}' "; |
| 83 | } |
| 84 | $strength = ''; |
| 85 | if(isset($attrs['strength']) && $attrs['strength'] === 1) { |
| 86 | $strength = '<div class="progress bg-gray-50" style="height: 5px;position: absolute;width: calc(100% - 40px);background: #eee;margin-top: -5px;"><div id="progressbar" class="progress-bar progress-bar-striped progress-bar-animated bg-danger" role="progressbar" style="width: 10%;height: 5px;" aria-valuenow="50" aria-valuemin="0" aria-valuemax="100"></div></div>'; |
| 87 | $strength .= '<script type="text/javascript" src="'.WPDM_ASSET_URL.'js/password-strength.js"></script>'; |
| 88 | } |
| 89 | return "<input type='password' {$_attrs} />{$strength}"; |
| 90 | } |
| 91 | |
| 92 | static function checkbox($attrs){ |
| 93 | $_attrs = ""; |
| 94 | if(isset($attrs['class'])) unset($attrs['class']); |
| 95 | $options = $attrs['options']; |
| 96 | unset($attrs['options']); |
| 97 | foreach ($attrs as $key => $value){ |
| 98 | $_attrs .= "{$key}='{$value}' "; |
| 99 | } |
| 100 | $_options = ""; |
| 101 | foreach ($options as $value => $label){ |
| 102 | $_options .= "<div><label class='d-block option-label'><input type='checkbox' $_attrs value='{$value}'> {$label}</label></div>\r\n"; |
| 103 | } |
| 104 | return $_options; |
| 105 | } |
| 106 | |
| 107 | static function radio($attrs){ |
| 108 | $_attrs = ""; |
| 109 | if(isset($attrs['class'])) unset($attrs['class']); |
| 110 | $options = $attrs['options']; |
| 111 | unset($attrs['options']); |
| 112 | foreach ($attrs as $key => $value){ |
| 113 | $_attrs .= "{$key}='{$value}' "; |
| 114 | } |
| 115 | $_options = ""; |
| 116 | foreach ($options as $value => $label){ |
| 117 | $_options .= "<div><label class='d-block option-label'><input type='radio' $_attrs value='{$value}'> {$label}</label></div>\r\n"; |
| 118 | } |
| 119 | return $_options; |
| 120 | } |
| 121 | |
| 122 | static function select($attrs, $value = '') |
| 123 | { |
| 124 | $_attrs = ""; |
| 125 | $attrs['class'] = isset($attrs['class']) ? "form-control " . $attrs['class'] : "form-control"; |
| 126 | $options = $attrs['options']; |
| 127 | unset($attrs['options']); |
| 128 | foreach ($attrs as $key => $_value) { |
| 129 | $_attrs .= "{$key}='{$_value}' "; |
| 130 | } |
| 131 | $_options = ""; |
| 132 | foreach ($options as $_value => $label) { |
| 133 | $_options .= "<option value='{$_value}' " . selected($_value, $value, false) . ">{$label}</option>\r\n"; |
| 134 | } |
| 135 | return "<select $_attrs>\r\n{$_options}\r\n</select>"; |
| 136 | } |
| 137 | |
| 138 | static function meidapicker($attrs, $value = '') |
| 139 | { |
| 140 | ob_start(); |
| 141 | $_attrs = ''; |
| 142 | if (is_array($attrs)) { |
| 143 | foreach ($attrs as $attr => $value) { |
| 144 | $_attrs .= "$attr='$value' "; |
| 145 | } |
| 146 | } |
| 147 | ?> |
| 148 | <div class="input-group"> |
| 149 | <input <?php echo $_attrs; ?> type="url" value="<?php echo $value; ?>"/> |
| 150 | <span class="input-group-append"> |
| 151 | <button class="btn btn-secondary btn-media-upload" type="button" rel="#<?php echo $attrs['id']; ?>"><i |
| 152 | class="far fa-image"></i></button> |
| 153 | </span> |
| 154 | </div> |
| 155 | <?php |
| 156 | return ob_get_clean(); |
| 157 | } |
| 158 | |
| 159 | static function reCaptcha($attrs){ |
| 160 | ob_start(); |
| 161 | ?> |
| 162 | <div class="form-group row"> |
| 163 | <div class="col-sm-12"> |
| 164 | <input type="hidden" id="<?php echo esc_attr($attrs['id']) ?>" name="<?php echo esc_attr($attrs['name']) ?>" value=""/> |
| 165 | <script src="https://www.google.com/recaptcha/enterprise.js?onload=onloadCallback&render=explicit" |
| 166 | async defer></script> |
| 167 | <div id="<?php echo esc_attr($attrs['id']) ?>_field" class="recap_field"></div> |
| 168 | <style> |
| 169 | .wp-core-ui .recap_field{ padding-bottom: 12px; } |
| 170 | .wp-core-ui #<?php echo esc_attr($attrs['id']) ?>_field iframe{ transform: scale(0.89); margin-left: -16px; margin-bottom: 12px; clear:both; } |
| 171 | .w3eden #<?php echo esc_attr($attrs['id']) ?>_field iframe{ transform: scale(1.16); margin-left: 24px; margin-top: 5px; margin-bottom: 5px; } |
| 172 | .w3eden #<?php echo esc_attr($attrs['id']) ?>_field{ padding-bottom: 10px !important; } |
| 173 | </style> |
| 174 | <script type="text/javascript"> |
| 175 | var verifyCallback = function (response) { |
| 176 | jQuery('#<?php echo esc_js($attrs['id']) ?>').val(response); |
| 177 | }; |
| 178 | var widgetId2; |
| 179 | var onloadCallback = function () { |
| 180 | grecaptcha.enterprise.render('<?php echo esc_js($attrs['id']) ?>_field', { |
| 181 | 'sitekey': '<?php echo esc_js(get_option('_wpdm_recaptcha_site_key')); ?>', |
| 182 | 'callback': verifyCallback, |
| 183 | 'theme': 'light' |
| 184 | }); |
| 185 | }; |
| 186 | </script> |
| 187 | </div> |
| 188 | |
| 189 | </div> |
| 190 | <?php |
| 191 | $captcha = ob_get_clean(); |
| 192 | return $captcha; |
| 193 | } |
| 194 | |
| 195 | static function custom($field, $attrs) |
| 196 | { |
| 197 | return call_user_func($field, $attrs); |
| 198 | } |
| 199 | |
| 200 | } |
| 201 |