| 1 |
<?php namespace TierPricingTable\Settings\CustomOptions; |
| 2 |
|
| 3 |
/** |
| 4 |
* Single-choice setting rendered as a group of choice chips. |
| 5 |
* |
| 6 |
* Each option is a native radio input, so the group keeps the browser's keyboard behaviour |
| 7 |
* (Tab into the group, arrow keys to move the selection) and works with the settings |
| 8 |
* dependency script, which reads the checked input by name. The selected chip shows a |
| 9 |
* check mark so the current value is visible without relying on colour alone. |
| 10 |
* |
| 11 |
* Field definition keys (on top of the WooCommerce ones): |
| 12 |
* - options: array of value => label. |
| 13 |
* - descriptions (optional): array of value => short text shown under the label of that chip. |
| 14 |
*/ |
| 15 |
class TPTDisplayType { |
| 16 |
|
| 17 |
const FIELD_TYPE = 'tpt_display_type'; |
| 18 |
|
| 19 |
public function __construct() { |
| 20 |
add_action( 'woocommerce_admin_field_' . self::FIELD_TYPE, array( $this, 'render' ) ); |
| 21 |
} |
| 22 |
|
| 23 |
public function render( $value ) { |
| 24 |
$extendedDescription = (string) ( $value['extended_description'] ?? '' ); |
| 25 |
$value = wp_parse_args( $value, array( |
| 26 |
'id' => '', |
| 27 |
'title' => isset( $value['name'] ) ? $value['name'] : '', |
| 28 |
'default' => '', |
| 29 |
'desc' => '', |
| 30 |
'options' => array(), |
| 31 |
'descriptions' => array(), |
| 32 |
'class' => '', |
| 33 |
) ); |
| 34 |
|
| 35 |
if ( ! isset( $value['value'] ) ) { |
| 36 |
$value['value'] = \WC_Admin_Settings::get_option( $value['id'], $value['default'] ); |
| 37 |
} |
| 38 |
|
| 39 |
$optionValue = $value['value']; |
| 40 |
$descriptions = is_array( $value['descriptions'] ) ? $value['descriptions'] : array(); |
| 41 |
$hasDesc = ! empty( array_filter( $descriptions ) ); |
| 42 |
|
| 43 |
?> |
| 44 |
<tr valign="top"> |
| 45 |
<th scope="row" class="titledesc"> |
| 46 |
<label for="<?php echo esc_attr( $value['id'] . '-' . array_key_first( $value['options'] ) ); ?>"> |
| 47 |
<?php echo esc_html( $value['title'] ); ?> |
| 48 |
</label> |
| 49 |
</th> |
| 50 |
<td class="forminp forminp-<?php echo esc_attr( sanitize_title( $value['type'] ) ); ?>"> |
| 51 |
<fieldset class="tpt-chips <?php echo esc_attr( $value['class'] ); ?><?php echo $hasDesc ? ' tpt-chips--described' : ''; ?>"> |
| 52 |
<legend class="screen-reader-text"><?php echo esc_html( $value['title'] ); ?></legend> |
| 53 |
<?php foreach ( $value['options'] as $key => $label ) : |
| 54 |
$inputId = $value['id'] . '-' . $key; |
| 55 |
?> |
| 56 |
<div class="tpt-chip"> |
| 57 |
<input type="radio" |
| 58 |
class="tpt-chip__input" |
| 59 |
name="<?php echo esc_attr( $value['id'] ); ?>" |
| 60 |
id="<?php echo esc_attr( $inputId ); ?>" |
| 61 |
value="<?php echo esc_attr( $key ); ?>" |
| 62 |
<?php checked( (string) $key, (string) $optionValue ); ?> |
| 63 |
> |
| 64 |
<label class="tpt-chip__surface" for="<?php echo esc_attr( $inputId ); ?>"> |
| 65 |
<span class="tpt-chip__check" aria-hidden="true"> |
| 66 |
<svg viewBox="0 0 20 20" width="16" height="16" focusable="false"> |
| 67 |
<path d="M7.6 14.2 3.9 10.5l1.4-1.4 2.3 2.3 7-7 1.4 1.4z" fill="currentColor"/> |
| 68 |
</svg> |
| 69 |
</span> |
| 70 |
<span class="tpt-chip__text"> |
| 71 |
<span class="tpt-chip__label"><?php echo esc_html( $label ); ?></span> |
| 72 |
<?php if ( ! empty( $descriptions[ $key ] ) ) : ?> |
| 73 |
<span class="tpt-chip__desc"><?php echo esc_html( $descriptions[ $key ] ); ?></span> |
| 74 |
<?php endif; ?> |
| 75 |
</span> |
| 76 |
</label> |
| 77 |
</div> |
| 78 |
<?php endforeach; ?> |
| 79 |
</fieldset> |
| 80 |
<?php if ( ! empty( $value['desc'] ) ) : ?> |
| 81 |
<p class="description"><?php echo esc_html( $value['desc'] ); ?></p> |
| 82 |
<?php endif; ?> |
| 83 |
<?php if ( isset( $value['extended_description'] ) ) : ?> |
| 84 |
<div class="tpt-toggle-extended-description"> |
| 85 |
<?php |
| 86 |
echo wp_kses_post( $extendedDescription ); // nosemgrep |
| 87 |
?> |
| 88 |
</div> |
| 89 |
<?php endif; ?> |
| 90 |
</td> |
| 91 |
</tr> |
| 92 |
<?php |
| 93 |
} |
| 94 |
} |
| 95 |
|