| 1 |
<?php namespace TierPricingTable\Settings\CustomOptions; |
| 2 |
|
| 3 |
/** |
| 4 |
* A list of checkboxes, or a multi-select, saved as a comma-separated string so the layout configurator's |
| 5 |
* hidden inputs can carry the same value. Unticking everything saves an empty string. |
| 6 |
*/ |
| 7 |
class TPTCheckboxListOption { |
| 8 |
|
| 9 |
const FIELD_TYPE = 'tpt_checkbox_list'; |
| 10 |
|
| 11 |
public function __construct() { |
| 12 |
add_action( 'woocommerce_admin_field_' . self::FIELD_TYPE, array( $this, 'render' ) ); |
| 13 |
|
| 14 |
add_filter( 'woocommerce_admin_settings_sanitize_option', function ( $value, $option, $rawValue ) { |
| 15 |
if ( self::FIELD_TYPE !== ( $option['type'] ?? '' ) ) { |
| 16 |
return $value; |
| 17 |
} |
| 18 |
|
| 19 |
$chosen = array_map( 'sanitize_text_field', array_filter( (array) $rawValue, 'is_scalar' ) ); |
| 20 |
|
| 21 |
// the category search has no fixed option list: keep term ids |
| 22 |
if ( 'category-search' === ( $option['display'] ?? '' ) ) { |
| 23 |
return implode( ',', array_filter( array_map( 'intval', $chosen ) ) ); |
| 24 |
} |
| 25 |
|
| 26 |
$known = array_map( 'strval', array_keys( (array) ( $option['options'] ?? array() ) ) ); |
| 27 |
|
| 28 |
return implode( ',', array_values( array_intersect( $chosen, $known ) ) ); |
| 29 |
}, 10, 3 ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* The stored value as a list. |
| 34 |
*/ |
| 35 |
public static function toArray( $value ): array { |
| 36 |
if ( is_array( $value ) ) { |
| 37 |
$value = implode( ',', $value ); |
| 38 |
} |
| 39 |
|
| 40 |
return array_values( array_filter( array_map( 'trim', explode( ',', (string) $value ) ), 'strlen' ) ); |
| 41 |
} |
| 42 |
|
| 43 |
public function render( $value ) { |
| 44 |
// the description is plugin-defined text (settings arrays), read once for output |
| 45 |
$description = (string) ( $value['desc'] ?? '' ); |
| 46 |
$value = wp_parse_args( $value, array( |
| 47 |
'id' => '', |
| 48 |
'title' => '', |
| 49 |
'desc' => '', |
| 50 |
'default' => '', |
| 51 |
'options' => array(), |
| 52 |
'display' => 'checkboxes', |
| 53 |
'placeholder' => '', |
| 54 |
'type' => self::FIELD_TYPE, |
| 55 |
) ); |
| 56 |
$chosen = self::toArray( \WC_Admin_Settings::get_option( $value['id'], $value['default'] ) ); |
| 57 |
?> |
| 58 |
<tr valign="top"> |
| 59 |
<th scope="row" class="titledesc"> |
| 60 |
<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label> |
| 61 |
</th> |
| 62 |
<td class="forminp forminp-<?php echo esc_attr( sanitize_title( $value['type'] ) ); ?>"> |
| 63 |
<input type="hidden" name="<?php echo esc_attr( $value['id'] ); ?>[]" value=""> |
| 64 |
<?php if ( 'category-search' === $value['display'] ) : ?> |
| 65 |
<select id="<?php echo esc_attr( $value['id'] ); ?>" name="<?php echo esc_attr( $value['id'] ); ?>[]" multiple="multiple" class="wc-category-search" style="width: 400px;" data-placeholder="<?php echo esc_attr( $value['placeholder'] ); ?>" data-action="woocommerce_json_search_categories" data-allow_clear="true"> |
| 66 |
<?php foreach ( $chosen as $termId ) : ?> |
| 67 |
<?php $term = get_term( (int) $termId, 'product_cat' ); ?> |
| 68 |
<?php if ( $term && ! is_wp_error( $term ) ) : ?> |
| 69 |
<option value="<?php echo esc_attr( $term->term_id ); ?>" selected="selected"><?php echo esc_html( $term->name ); ?></option> |
| 70 |
<?php endif; ?> |
| 71 |
<?php endforeach; ?> |
| 72 |
</select> |
| 73 |
<?php elseif ( 'select' === $value['display'] ) : ?> |
| 74 |
<select id="<?php echo esc_attr( $value['id'] ); ?>" name="<?php echo esc_attr( $value['id'] ); ?>[]" multiple="multiple" class="wc-enhanced-select" style="width: 400px;" data-placeholder="<?php echo esc_attr( $value['placeholder'] ); ?>"> |
| 75 |
<?php foreach ( $value['options'] as $key => $label ) : ?> |
| 76 |
<option value="<?php echo esc_attr( $key ); ?>" <?php selected( in_array( (string) $key, $chosen, true ) ); ?>><?php echo esc_html( $label ); ?></option> |
| 77 |
<?php endforeach; ?> |
| 78 |
</select> |
| 79 |
<?php else : ?> |
| 80 |
<fieldset id="<?php echo esc_attr( $value['id'] ); ?>" class="tpt-checkbox-list"> |
| 81 |
<?php foreach ( $value['options'] as $key => $label ) : ?> |
| 82 |
<label style="display: block; margin-bottom: 4px;"> |
| 83 |
<input type="checkbox" name="<?php echo esc_attr( $value['id'] ); ?>[]" value="<?php echo esc_attr( $key ); ?>" <?php checked( in_array( (string) $key, $chosen, true ) ); ?>> |
| 84 |
<?php echo esc_html( $label ); ?> |
| 85 |
</label> |
| 86 |
<?php endforeach; ?> |
| 87 |
</fieldset> |
| 88 |
<?php endif; ?> |
| 89 |
<?php if ( $value['desc'] ) : ?> |
| 90 |
<p class="description"><?php echo wp_kses_post( $description ); // nosemgrep ?></p> |
| 91 |
<?php endif; ?> |
| 92 |
</td> |
| 93 |
</tr> |
| 94 |
<?php |
| 95 |
} |
| 96 |
} |
| 97 |
|