| @@ -16,25 +16,49 @@ | ||
| 16 | 16 | * |
| 17 | 17 | * @param string $id |
| 18 | 18 | * @param string $name |
| 19 | 19 | * @param array $args { |
| 20 | - * @param bool|null $echo True if you want the toggle to echo. False if you want it to return an HTML string. | |
| 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. | |
| 21 | 23 | * } |
| 22 | 24 | * |
| 23 | - * @return string|void | |
| 25 | + * @return string|null | |
| 24 | 26 | */ |
| 25 | 27 | public static function toggle( $id, $name, $args ) { |
| 26 | 28 | wp_enqueue_script( 'formidable_settings' ); |
| 27 | 29 | return FrmAppHelper::clip( |
| 28 | 30 | // @phpstan-ignore-next-line |
| 29 | - function() use ( $id, $name, $args ) { | |
| 31 | + function () use ( $id, $name, $args ) { | |
| 30 | 32 | require FrmAppHelper::plugin_path() . '/classes/views/shared/toggle.php'; |
| 31 | 33 | }, |
| 32 | - isset( $args['echo'] ) ? $args['echo'] : false | |
| 34 | + $args['echo'] ?? false | |
| 33 | 35 | ); |
| 34 | 36 | } |
| 35 | 37 | |
| 36 | 38 | /** |
| 39 | + * Show readonly setting icon | |
| 40 | + * | |
| 41 | + * @since 6.33 | |
| 42 | + * | |
| 43 | + * @param bool $is_enabled Whether the setting is enabled. | |
| 44 | + * | |
| 45 | + * @return void | |
| 46 | + */ | |
| 47 | + public static function show_readonly_setting_icon( $is_enabled ) { | |
| 48 | + if ( $is_enabled ) { | |
| 49 | + echo '<span class="frm-success-icon">'; | |
| 50 | + FrmAppHelper::icon_by_class( 'frmfont frm_checkmark_icon', array( 'aria-hidden' => 'true' ) ); | |
| 51 | + echo '</span>'; | |
| 52 | + return; | |
| 53 | + } | |
| 54 | + | |
| 55 | + echo '<span class="frm-x-icon">'; | |
| 56 | + FrmAppHelper::icon_by_class( 'frmfont frm_x_icon', array( 'aria-hidden' => 'true' ) ); | |
| 57 | + echo '</span>'; | |
| 58 | + } | |
| 59 | + | |
| 60 | + /** | |
| 37 | 61 | * Echo a dropdown option. |
| 38 | 62 | * This is useful to avoid closing and opening PHP to echo <option> tags which leads to extra whitespace. |
| 39 | 63 | * Avoiding whitespace saves 5KB of HTML for an international address field with a country dropdown with 252 options. |
| 40 | 64 | * |
| @@ -42,8 +66,9 @@ | ||
| 42 | 66 | * |
| 43 | 67 | * @param string $option The string used as the option label. |
| 44 | 68 | * @param bool $selected True if the option should be selected. |
| 45 | 69 | * @param array $params Other HTML params for the option. |
| 70 | + * | |
| 46 | 71 | * @return void |
| 47 | 72 | */ |
| 48 | 73 | public static function echo_dropdown_option( $option, $selected, $params = array() ) { |
| 49 | 74 | echo '<option '; |
| @@ -51,6 +76,81 @@ | ||
| 51 | 76 | selected( $selected ); |
| 52 | 77 | echo '>'; |
| 53 | 78 | echo esc_html( $option === '' ? ' ' : $option ); |
| 54 | 79 | echo '</option>'; |
| 80 | + } | |
| 81 | + | |
| 82 | + /** | |
| 83 | + * Renders a number input with unit selector. | |
| 84 | + * | |
| 85 | + * @since 6.24 | |
| 86 | + * | |
| 87 | + * @param array $args { | |
| 88 | + * Optional. Arguments to customize the unit input. | |
| 89 | + * | |
| 90 | + * @type array $field_attrs Attributes for the hidden input storing the field value. | |
| 91 | + * @type array $input_number_attrs Attributes for the visible number input. | |
| 92 | + * @type array $units Available units for selection. Default is ['px', '%', 'em']. | |
| 93 | + * @type string $value Initial value with optional unit (e.g. '10px', '50%'). | |
| 94 | + * } | |
| 95 | + * | |
| 96 | + * @return void | |
| 97 | + */ | |
| 98 | + public static function echo_unit_input( $args = array() ) { | |
| 99 | + $args = wp_parse_args( | |
| 100 | + $args, | |
| 101 | + array( | |
| 102 | + 'field_attrs' => array(), | |
| 103 | + 'input_number_attrs' => array(), | |
| 104 | + 'units' => array( '', 'px', '%', 'em' ), | |
| 105 | + 'default_unit' => 'px', | |
| 106 | + 'value' => '', | |
| 107 | + ) | |
| 108 | + ); | |
| 109 | + | |
| 110 | + $units = $args['units']; | |
| 111 | + $value = $args['value']; | |
| 112 | + | |
| 113 | + // Extract unit and number value if value is a string | |
| 114 | + if ( '' !== $value && ! is_numeric( $value ) ) { | |
| 115 | + $pattern = '/^([0-9.]*)(' . implode( '|', array_map( 'preg_quote', $units ) ) . ')?$/'; | |
| 116 | + preg_match( $pattern, $value, $matches ); | |
| 117 | + $selected_unit = $matches[2] ?? ''; | |
| 118 | + | |
| 119 | + if ( ! empty( $matches[1] ) ) { | |
| 120 | + $value = $matches[1]; | |
| 121 | + } | |
| 122 | + } | |
| 123 | + | |
| 124 | + $input_number_attrs = array_merge( | |
| 125 | + $args['input_number_attrs'], | |
| 126 | + array( | |
| 127 | + 'type' => ! empty( $selected_unit ) ? 'number' : 'text', | |
| 128 | + 'value' => $value, | |
| 129 | + 'class' => trim( 'frm-unit-input-control ' . ( $args['input_number_attrs']['class'] ?? '' ) ), | |
| 130 | + ) | |
| 131 | + ); | |
| 132 | + | |
| 133 | + $hidden_value = $args['value']; | |
| 134 | + | |
| 135 | + if ( is_numeric( $hidden_value ) ) { | |
| 136 | + $hidden_value .= $args['default_unit']; | |
| 137 | + } | |
| 138 | + // phpcs:disable Generic.WhiteSpace.ScopeIndent | |
| 139 | + ?> | |
| 140 | + <span class="frm-unit-input"> | |
| 141 | + <input type="hidden" value="<?php echo esc_attr( $hidden_value ); ?>" <?php FrmAppHelper::array_to_html_params( $args['field_attrs'], true ); ?> /> | |
| 142 | + <input <?php FrmAppHelper::array_to_html_params( $input_number_attrs, true ); ?> /> | |
| 143 | + <span class="frm-input-group-suffix"> | |
| 144 | + <select aria-label="<?php esc_attr_e( 'Select unit', 'formidable' ); ?>" tabindex="0"> | |
| 145 | + <?php | |
| 146 | + foreach ( $units as $unit ) { | |
| 147 | + self::echo_dropdown_option( $unit, $unit === ( $selected_unit ?? $args['default_unit'] ), array( 'value' => $unit ) ); | |
| 148 | + } | |
| 149 | + ?> | |
| 150 | + </select> | |
| 151 | + </span> | |
| 152 | + </span> | |
| 153 | + <?php | |
| 154 | + // phpcs:enable Generic.WhiteSpace.ScopeIndent | |
| 55 | 155 | } |
| 56 | 156 | } |