| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* @since 6.0 |
| 8 |
*/ |
| 9 |
class FrmHtmlHelper { |
| 10 |
|
| 11 |
/** |
| 12 |
* Create a toggle and either echo or return the string. |
| 13 |
* This is intended for use on admin pages only. The CSS is included in frm_admin.css. |
| 14 |
* |
| 15 |
* @since 6.0 |
| 16 |
* |
| 17 |
* @param string $id |
| 18 |
* @param string $name |
| 19 |
* @param array $args { |
| 20 |
* Any extra arguments. |
| 21 |
* |
| 22 |
* @type bool|null $echo True if you want the toggle to echo. False if you want it to return an HTML string. |
| 23 |
* } |
| 24 |
* |
| 25 |
* @return string|null |
| 26 |
*/ |
| 27 |
public static function toggle( $id, $name, $args ) { |
| 28 |
wp_enqueue_script( 'formidable_settings' ); |
| 29 |
return FrmAppHelper::clip( |
| 30 |
// @phpstan-ignore-next-line |
| 31 |
function () use ( $id, $name, $args ) { |
| 32 |
require FrmAppHelper::plugin_path() . '/classes/views/shared/toggle.php'; |
| 33 |
}, |
| 34 |
$args['echo'] ?? false |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Echo a dropdown option. |
| 40 |
* This is useful to avoid closing and opening PHP to echo <option> tags which leads to extra whitespace. |
| 41 |
* Avoiding whitespace saves 5KB of HTML for an international address field with a country dropdown with 252 options. |
| 42 |
* |
| 43 |
* @since 6.3.1 |
| 44 |
* |
| 45 |
* @param string $option The string used as the option label. |
| 46 |
* @param bool $selected True if the option should be selected. |
| 47 |
* @param array $params Other HTML params for the option. |
| 48 |
* |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public static function echo_dropdown_option( $option, $selected, $params = array() ) { |
| 52 |
echo '<option '; |
| 53 |
FrmAppHelper::array_to_html_params( $params, true ); |
| 54 |
selected( $selected ); |
| 55 |
echo '>'; |
| 56 |
echo esc_html( $option === '' ? ' ' : $option ); |
| 57 |
echo '</option>'; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Renders a number input with unit selector. |
| 62 |
* |
| 63 |
* @since 6.24 |
| 64 |
* |
| 65 |
* @param array $args { |
| 66 |
* Optional. Arguments to customize the unit input. |
| 67 |
* |
| 68 |
* @type array $field_attrs Attributes for the hidden input storing the field value. |
| 69 |
* @type array $input_number_attrs Attributes for the visible number input. |
| 70 |
* @type array $units Available units for selection. Default is ['px', '%', 'em']. |
| 71 |
* @type string $value Initial value with optional unit (e.g. '10px', '50%'). |
| 72 |
* } |
| 73 |
* |
| 74 |
* @return void |
| 75 |
*/ |
| 76 |
public static function echo_unit_input( $args = array() ) { |
| 77 |
$args = wp_parse_args( |
| 78 |
$args, |
| 79 |
array( |
| 80 |
'field_attrs' => array(), |
| 81 |
'input_number_attrs' => array(), |
| 82 |
'units' => array( '', 'px', '%', 'em' ), |
| 83 |
'default_unit' => 'px', |
| 84 |
'value' => '', |
| 85 |
) |
| 86 |
); |
| 87 |
|
| 88 |
$units = $args['units']; |
| 89 |
$value = $args['value']; |
| 90 |
|
| 91 |
// Extract unit and number value if value is a string |
| 92 |
if ( '' !== $value && ! is_numeric( $value ) ) { |
| 93 |
$pattern = '/^([0-9.]*)(' . implode( '|', array_map( 'preg_quote', $units ) ) . ')?$/'; |
| 94 |
preg_match( $pattern, $value, $matches ); |
| 95 |
$selected_unit = $matches[2] ?? ''; |
| 96 |
|
| 97 |
if ( ! empty( $matches[1] ) ) { |
| 98 |
$value = $matches[1]; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
$input_number_attrs = array_merge( |
| 103 |
$args['input_number_attrs'], |
| 104 |
array( |
| 105 |
'type' => ! empty( $selected_unit ) ? 'number' : 'text', |
| 106 |
'value' => $value, |
| 107 |
'class' => trim( 'frm-unit-input-control ' . ( $args['input_number_attrs']['class'] ?? '' ) ), |
| 108 |
) |
| 109 |
); |
| 110 |
|
| 111 |
$hidden_value = $args['value']; |
| 112 |
|
| 113 |
if ( is_numeric( $hidden_value ) ) { |
| 114 |
$hidden_value .= $args['default_unit']; |
| 115 |
} |
| 116 |
?> |
| 117 |
<span class="frm-unit-input"> |
| 118 |
<input type="hidden" value="<?php echo esc_attr( $hidden_value ); ?>" <?php FrmAppHelper::array_to_html_params( $args['field_attrs'], true ); ?> /> |
| 119 |
<input <?php FrmAppHelper::array_to_html_params( $input_number_attrs, true ); ?> /> |
| 120 |
<span class="frm-input-group-suffix"> |
| 121 |
<select aria-label="<?php echo esc_attr__( 'Select unit', 'formidable' ); ?>" tabindex="0"> |
| 122 |
<?php |
| 123 |
foreach ( $units as $unit ) { |
| 124 |
self::echo_dropdown_option( $unit, $unit === ( $selected_unit ?? $args['default_unit'] ), array( 'value' => $unit ) ); |
| 125 |
} |
| 126 |
?> |
| 127 |
</select> |
| 128 |
</span> |
| 129 |
</span> |
| 130 |
<?php |
| 131 |
} |
| 132 |
} |
| 133 |
|