| 1 |
<?php |
| 2 |
/** |
| 3 |
* Form field attributes. |
| 4 |
* |
| 5 |
* @package ghostkit |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Class GhostKit_Form_Field_Attributes |
| 14 |
*/ |
| 15 |
class GhostKit_Form_Field_Attributes { |
| 16 |
/** |
| 17 |
* Prepare attributes array for registering block. |
| 18 |
* |
| 19 |
* @param array $attributes array with custom attributes. |
| 20 |
*/ |
| 21 |
public static function get_block_attributes( $attributes ) { |
| 22 |
$default_attributes = array( |
| 23 |
'slug' => array( |
| 24 |
'type' => 'string', |
| 25 |
), |
| 26 |
|
| 27 |
'label' => array( |
| 28 |
'type' => 'string', |
| 29 |
'default' => esc_html__( 'Field Label', 'ghostkit' ), |
| 30 |
), |
| 31 |
'description' => array( |
| 32 |
'type' => 'string', |
| 33 |
), |
| 34 |
'descriptionHidden' => array( |
| 35 |
'type' => 'boolean', |
| 36 |
), |
| 37 |
'placeholder' => array( |
| 38 |
'type' => 'string', |
| 39 |
), |
| 40 |
'default' => array( |
| 41 |
'type' => 'string', |
| 42 |
), |
| 43 |
'hideLabel' => array( |
| 44 |
'type' => 'boolean', |
| 45 |
), |
| 46 |
'hideDescription' => array( |
| 47 |
'type' => 'boolean', |
| 48 |
), |
| 49 |
'required' => array( |
| 50 |
'type' => 'boolean', |
| 51 |
), |
| 52 |
|
| 53 |
'className' => array( |
| 54 |
'type' => 'string', |
| 55 |
), |
| 56 |
); |
| 57 |
|
| 58 |
if ( ! empty( $default_attributes ) ) { |
| 59 |
foreach ( $default_attributes as $k => $val ) { |
| 60 |
if ( isset( $attributes[ $k ] ) ) { |
| 61 |
$attributes[ $k ] = array_merge( |
| 62 |
$val, |
| 63 |
$attributes[ $k ] |
| 64 |
); |
| 65 |
} else { |
| 66 |
$attributes[ $k ] = $val; |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
return $attributes; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Prepare attributes string for fields. |
| 76 |
* |
| 77 |
* @param array $attributes attributes list. |
| 78 |
*/ |
| 79 |
public static function get( $attributes ) { |
| 80 |
$allowed_attributes = array( |
| 81 |
'id', |
| 82 |
'placeholder', |
| 83 |
'default', |
| 84 |
'min', |
| 85 |
'max', |
| 86 |
'step', |
| 87 |
'multiple', |
| 88 |
'required', |
| 89 |
'for', |
| 90 |
'name', |
| 91 |
); |
| 92 |
|
| 93 |
if ( isset( $attributes['slug'] ) ) { |
| 94 |
$attributes['name'] = $attributes['slug']; |
| 95 |
} |
| 96 |
|
| 97 |
if ( isset( $attributes['slug'] ) && ! isset( $attributes['id'] ) ) { |
| 98 |
$attributes['id'] = $attributes['slug']; |
| 99 |
} |
| 100 |
|
| 101 |
if ( $attributes && ! empty( $attributes ) ) { |
| 102 |
foreach ( $attributes as $k => $val ) { |
| 103 |
if ( ! empty( $val ) && '' !== $val ) { |
| 104 |
if ( ! in_array( $k, $allowed_attributes, true ) ) { |
| 105 |
continue; |
| 106 |
} |
| 107 |
|
| 108 |
// boolean value. |
| 109 |
if ( is_bool( $val ) ) { |
| 110 |
if ( $val ) { |
| 111 |
$val = $k; |
| 112 |
} else { |
| 113 |
continue; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
// default attribute. |
| 118 |
if ( 'default' === $k ) { |
| 119 |
$k = 'value'; |
| 120 |
} |
| 121 |
|
| 122 |
// value attribute. |
| 123 |
if ( 'name' === $k ) { |
| 124 |
if ( isset( $attributes['slug_sub'] ) ) { |
| 125 |
$val .= '[' . $attributes['slug_sub'] . ']'; |
| 126 |
} else { |
| 127 |
$val .= '[value]'; |
| 128 |
} |
| 129 |
|
| 130 |
if ( isset( $attributes['fieldIsArray'] ) && $attributes['fieldIsArray'] ) { |
| 131 |
$val .= '[]'; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
echo ' ' . esc_attr( $k ) . '="' . esc_attr( $val ) . '"'; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
new GhostKit_Form_Field_Attributes(); |
| 142 |
|