| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Exit if accessed directly |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Prints help text |
| 9 |
* |
| 10 |
* @param string the description |
| 11 |
* |
| 12 |
* @return void |
| 13 |
*/ |
| 14 |
function erp_html_form_help( $value = '' ) { |
| 15 |
if ( ! empty( $value ) ) { |
| 16 |
echo '<span class="description">' . wp_kses_post( $value ) . '</span>'; |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Prints a label attribute |
| 22 |
* |
| 23 |
* @param string label vlaue |
| 24 |
* @param string id field for label |
| 25 |
* |
| 26 |
* @return void |
| 27 |
*/ |
| 28 |
function erp_html_form_label( $label, $field_id = '', $required = false ) { |
| 29 |
$req = $required ? ' <span class="required">*</span>' : ''; |
| 30 |
echo '<label for="' . esc_attr( $field_id ) . '">' . wp_kses_post( $label ) . $req . '</label>'; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Handles an elements custom attribute |
| 35 |
* |
| 36 |
* @param array attributes as key/value pair |
| 37 |
* |
| 38 |
* @return array |
| 39 |
*/ |
| 40 |
function erp_html_form_custom_attr( $attr = array(), $other_attr = array() ) { |
| 41 |
$custom_attributes = array(); |
| 42 |
|
| 43 |
if ( ! empty( $attr ) && is_array( $attr ) ) { |
| 44 |
foreach ( $attr as $attribute => $value ) { |
| 45 |
if ( $value != '' ) { |
| 46 |
$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"'; |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
if ( ! empty( $other_attr ) && is_array( $other_attr ) ) { |
| 52 |
foreach ( $attr as $attribute => $value ) { |
| 53 |
$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"'; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
return $custom_attributes; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Prints a HTML input field |
| 62 |
* |
| 63 |
* @param array the args |
| 64 |
* |
| 65 |
* @return void |
| 66 |
*/ |
| 67 |
function erp_html_form_input( $args = array() ) { |
| 68 |
$defaults = array( |
| 69 |
'placeholder' => '', |
| 70 |
'required' => false, |
| 71 |
'type' => 'text', |
| 72 |
'class' => '', |
| 73 |
'tag' => '', |
| 74 |
'wrapper_class' => '', |
| 75 |
'label' => '', |
| 76 |
'name' => '', |
| 77 |
'id' => '', |
| 78 |
'value' => '', |
| 79 |
'help' => '', |
| 80 |
'addon' => '', |
| 81 |
'addon_pos' => 'before', |
| 82 |
'custom_attr' => array(), |
| 83 |
'options' => array(), |
| 84 |
); |
| 85 |
|
| 86 |
$field = wp_parse_args( $args, $defaults ); |
| 87 |
$field_id = empty( $field['id'] ) ? $field['name'] : $field['id']; |
| 88 |
|
| 89 |
$field_attributes = array_merge( array( |
| 90 |
'name' => $field['name'], |
| 91 |
'id' => $field_id, |
| 92 |
'class' => $field['class'], |
| 93 |
'placeholder' => $field['placeholder'], |
| 94 |
), $field['custom_attr'] ); |
| 95 |
|
| 96 |
if ( $field['required'] ) { |
| 97 |
$field_attributes['required'] = 'required'; |
| 98 |
} |
| 99 |
|
| 100 |
$custom_attributes = erp_html_form_custom_attr( $field_attributes ); |
| 101 |
|
| 102 |
// open tag |
| 103 |
if ( ! empty( $field['tag'] ) ) { |
| 104 |
echo '<' . $field['tag'] . ' class="erp-form-field ' . esc_attr( $field['name'] ) . '_field ' . esc_attr( $field['wrapper_class'] ) . '">'; |
| 105 |
} |
| 106 |
|
| 107 |
if ( ! empty( $field['label'] ) ) { |
| 108 |
erp_html_form_label( $field['label'], $field_id, $field['required'] ); |
| 109 |
} |
| 110 |
|
| 111 |
if ( ! empty( $field['addon'] ) ) { |
| 112 |
echo '<div class="input-group">'; |
| 113 |
|
| 114 |
if ( $field['addon_pos'] == 'before' ) { |
| 115 |
echo '<span class="input-group-addon">' . $field['addon'] . '</span>'; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
switch ( $field['type'] ) { |
| 120 |
case 'text': |
| 121 |
case 'email': |
| 122 |
case 'number': |
| 123 |
case 'hidden': |
| 124 |
case 'url': |
| 125 |
echo '<input type="' . $field['type'] . '" value="' . esc_attr( $field['value'] ) . '" ' . implode( ' ', $custom_attributes ) . ' />'; |
| 126 |
break; |
| 127 |
|
| 128 |
case 'select': |
| 129 |
if ( $field['options'] ) { |
| 130 |
echo '<select ' . implode( ' ', $custom_attributes ) . '>'; |
| 131 |
foreach ($field['options'] as $key => $value) { |
| 132 |
printf( "<option value='%s'%s>%s</option>\n", $key, selected( $field['value'], $key, false ), $value ); |
| 133 |
} |
| 134 |
echo '</select>'; |
| 135 |
} |
| 136 |
break; |
| 137 |
|
| 138 |
case 'textarea': |
| 139 |
echo '<textarea ' . implode( ' ', $custom_attributes ) . '>' . esc_textarea( $field['value'] ) . '</textarea>'; |
| 140 |
break; |
| 141 |
|
| 142 |
case 'wysiwyg': |
| 143 |
$editor_args = [ |
| 144 |
'editor_class' => $field['class'], |
| 145 |
'textarea_rows' => isset( $field['custom_attr']['rows'] ) ? $field['custom_attr']['rows'] : 10, |
| 146 |
'media_buttons' => isset( $field['custom_attr']['media'] ) ? $field['custom_attr']['media'] : false, |
| 147 |
'teeny' => isset( $field['custom_attr']['teeny'] ) ? $field['custom_attr']['teeny'] : true, |
| 148 |
|
| 149 |
]; |
| 150 |
|
| 151 |
wp_editor( $field['value'], $field['name'], $editor_args ); |
| 152 |
break; |
| 153 |
|
| 154 |
case 'checkbox': |
| 155 |
//echo '<input type="hidden" value="off" name="' . $field['name'] . '" />'; |
| 156 |
echo '<span class="checkbox">'; |
| 157 |
echo '<label for="' . esc_attr( $field_attributes['id'] ) . '">'; |
| 158 |
echo '<input type="checkbox" '.checked( $field['value'], 'on', false ).' value="on" ' . implode( ' ', $custom_attributes ) . ' />'; |
| 159 |
echo wp_kses_post( $field['help'] ); |
| 160 |
echo '</label>'; |
| 161 |
echo '</span>'; |
| 162 |
break; |
| 163 |
|
| 164 |
case 'multicheckbox': |
| 165 |
|
| 166 |
echo '<span class="checkbox">'; |
| 167 |
unset( $custom_attributes['id'] ); |
| 168 |
|
| 169 |
foreach ( $field['options'] as $key => $value ) { |
| 170 |
echo '<label for="' . esc_attr( $field_attributes['id'] ) . '-' . $key .'">'; |
| 171 |
if ( ! empty( $field['value'] ) ) { |
| 172 |
if ( is_array( $field['value'] ) ) { |
| 173 |
$checked = in_array( $key, $field['value'] ) ? 'checked' : ''; |
| 174 |
} else if ( is_string( $field['value'] ) ) { |
| 175 |
$checked = in_array( $key, explode(',', $field['value'] ) ) ? 'checked' : ''; |
| 176 |
} else { |
| 177 |
$checked = ''; |
| 178 |
} |
| 179 |
} else { |
| 180 |
$checked = ''; |
| 181 |
} |
| 182 |
|
| 183 |
echo '<input type="checkbox" '. $checked .' id="' . esc_attr( $field_attributes['id'] ) . '-' . $key . '" value="'.$key.'" ' . implode( ' ', $custom_attributes ) . ' />'; |
| 184 |
echo '<span class="checkbox-value">' . wp_kses_post( $value ) . '</span>'; |
| 185 |
echo '</label>'; |
| 186 |
} |
| 187 |
echo '</span>'; |
| 188 |
break; |
| 189 |
|
| 190 |
case 'radio': |
| 191 |
echo '<span class="checkbox">'; |
| 192 |
if ( $field['options'] ) { |
| 193 |
foreach ( $field['options'] as $key => $value) { |
| 194 |
echo '<input type="radio" '.checked( $field['value'], $key, false ).' value="'.$key.'" ' . implode( ' ', $custom_attributes ) . ' id="'. esc_attr( $field_attributes['id'] ) . '-' . $key . '"/>'. $value . ' <br><br>'; |
| 195 |
} |
| 196 |
} |
| 197 |
echo '</span>'; |
| 198 |
break; |
| 199 |
|
| 200 |
case 'file': |
| 201 |
//need to enqueue wp_enqueue_script( 'plupload-handlers' ); wp_enqueue_script( 'erp-file-upload' ); |
| 202 |
$id = $field['id']; |
| 203 |
$pick_files = $id . '-upload-pickfiles'; |
| 204 |
$drop = $id . '-drop-files'; |
| 205 |
$action = isset( $field['action'] ) ? $field['action'] : 'erp_file_upload'; |
| 206 |
$call_back = isset( $field['callback'] ) ? json_encode( $field['callback'] ) : json_encode([]); |
| 207 |
$values = is_array( $field['value'] ) ? $field['value'] : []; |
| 208 |
?> |
| 209 |
|
| 210 |
<div id="<?php echo $id; ?>" class="erp-attachment-area"> |
| 211 |
|
| 212 |
<div id="<?php echo $drop; ?>" class="erp-drop-jon"> |
| 213 |
<div class="erp-attachment-upload-filelist" data-type="file"> |
| 214 |
<ul class="erp-attachment-list"> |
| 215 |
<?php |
| 216 |
$uploader = new \WeDevs\ERP\Uploader(); |
| 217 |
foreach ( $values as $key => $attach_id ) { |
| 218 |
echo $uploader->attach_html( $attach_id, $custom_attributes ); |
| 219 |
} |
| 220 |
?> |
| 221 |
|
| 222 |
</ul> |
| 223 |
<div class="erp-clear"></div> |
| 224 |
|
| 225 |
<div class="erp-attc-link-text"><?php _e( 'To attach, ', 'erp' ); ?> <a id="<?php echo $pick_files; ?>" href="#"><?php _e( 'select files', 'erp' ); ?></a><?php _e( ' from your computer.', 'erp' ); ?></div> |
| 226 |
</div> |
| 227 |
</div> |
| 228 |
</div> |
| 229 |
|
| 230 |
|
| 231 |
<script type="text/javascript"> |
| 232 |
jQuery(function($) { |
| 233 |
var pick_files = '<?php echo $pick_files; ?>', |
| 234 |
id = '<?php echo $id; ?>', |
| 235 |
drop_jone = '<?php echo $drop; ?>', |
| 236 |
action = '<?php echo $action; ?>', |
| 237 |
callback = <?php echo $call_back; ?>; |
| 238 |
|
| 239 |
new ERP_Uploader( action, pick_files, id, drop_jone, 'file_upload', 'doc,docx,xls,xlsx,jpg,jpeg,gif,png,pdf,bmp,zip,rar', 1024, callback ); |
| 240 |
}); |
| 241 |
</script> |
| 242 |
<?php |
| 243 |
break; |
| 244 |
|
| 245 |
default: |
| 246 |
# code... |
| 247 |
break; |
| 248 |
} |
| 249 |
|
| 250 |
if ( ! empty( $field['addon'] ) ) { |
| 251 |
|
| 252 |
if ( $field['addon_pos'] == 'after' ) { |
| 253 |
echo '<span class="input-group-addon">' . $field['addon'] . '</span>'; |
| 254 |
} |
| 255 |
|
| 256 |
echo '</div>'; |
| 257 |
} |
| 258 |
|
| 259 |
if ( $field['type'] != 'checkbox' ) { |
| 260 |
erp_html_form_help( $field['help'] ); |
| 261 |
} |
| 262 |
|
| 263 |
// closing tag |
| 264 |
if ( ! empty( $field['tag'] ) ) { |
| 265 |
echo '</' . $field['tag'] . '>'; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Generate an HTML dropdown option by provided values |
| 271 |
* |
| 272 |
* @param array $values |
| 273 |
* @param string $selected |
| 274 |
* |
| 275 |
* @return string |
| 276 |
*/ |
| 277 |
function erp_html_generate_dropdown( $values = array(), $selected = null ) { |
| 278 |
$dropdown = ''; |
| 279 |
|
| 280 |
if ( $values ) { |
| 281 |
foreach ($values as $key => $label) { |
| 282 |
$dropdown .= sprintf( "<option value='%s'%s>%s</option>\n", $key, selected( $selected, $key, false ), $label ); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
return $dropdown; |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Print notices for WordPress |
| 291 |
* |
| 292 |
* @param string $text |
| 293 |
* @param string $type |
| 294 |
* |
| 295 |
* @return void |
| 296 |
*/ |
| 297 |
function erp_html_show_notice( $text, $type = 'updated' ) { |
| 298 |
?> |
| 299 |
<div class="<?php echo esc_attr( $type ); ?>"> |
| 300 |
<p><strong><?php echo $text; ?></strong></p> |
| 301 |
</div> |
| 302 |
<?php |
| 303 |
} |
| 304 |
|