| 1 |
<?php namespace TierPricingTable\Addons\NonLoggedInUsers\Visibility\Admin; |
| 2 |
|
| 3 |
use TierPricingTable\Addons\NonLoggedInUsers\Visibility\VisibilityMeta; |
| 4 |
use TierPricingTable\Addons\NonLoggedInUsers\Visibility\VisibilityRule; |
| 5 |
|
| 6 |
/** |
| 7 |
* The roles checklist under a visibility mode select, shown only while the mode names roles. |
| 8 |
*/ |
| 9 |
class RoleChecklist { |
| 10 |
|
| 11 |
/** |
| 12 |
* @param string $name input name (posted as an array) |
| 13 |
* @param string[] $selected |
| 14 |
* @param string $modeFieldId id of the mode select that controls the checklist |
| 15 |
* @param bool $termForm true for the taxonomy screens (table-row markup), false for the product panel |
| 16 |
*/ |
| 17 |
public static function render( string $name, array $selected, string $modeFieldId, bool $termForm = false ) { |
| 18 |
$options = VisibilityMeta::getRoleOptions(); |
| 19 |
$id = $name . '_list'; |
| 20 |
?> |
| 21 |
<?php if ( $termForm ) : ?> |
| 22 |
<tr class="form-field tpt-visibility-roles" data-mode-field="<?php echo esc_attr( $modeFieldId ); ?>"> |
| 23 |
<th scope="row"><?php esc_html_e( 'Roles', 'tier-pricing-table' ); ?></th> |
| 24 |
<td> |
| 25 |
<?php else : ?> |
| 26 |
<p class="form-field tpt-visibility-roles" data-mode-field="<?php echo esc_attr( $modeFieldId ); ?>"> |
| 27 |
<label for="<?php echo esc_attr( $id ); ?>"><?php esc_html_e( 'Roles', 'tier-pricing-table' ); ?></label> |
| 28 |
<?php endif; ?> |
| 29 |
<span id="<?php echo esc_attr( $id ); ?>" class="tpt-visibility-roles__list"> |
| 30 |
<?php foreach ( $options as $role => $label ) : ?> |
| 31 |
<label class="tpt-visibility-roles__item"> |
| 32 |
<input type="checkbox" name="<?php echo esc_attr( $name ); ?>[]" value="<?php echo esc_attr( $role ); ?>" <?php checked( in_array( $role, $selected, true ) ); ?>> |
| 33 |
<?php echo esc_html( $label ); ?> |
| 34 |
</label> |
| 35 |
<?php endforeach; ?> |
| 36 |
</span> |
| 37 |
<?php if ( $termForm ) : ?> |
| 38 |
<p class="description"><?php esc_html_e( 'The rule also covers the child categories. A product can override it in its Tiered Pricing tab.', 'tier-pricing-table' ); ?></p> |
| 39 |
</td> |
| 40 |
</tr> |
| 41 |
<?php else : ?> |
| 42 |
</p> |
| 43 |
<?php endif; ?> |
| 44 |
<?php self::printAssets(); ?> |
| 45 |
<?php |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* The styles and the show/hide script, once per page. |
| 50 |
*/ |
| 51 |
public static function printAssets() { |
| 52 |
static $printed = false; |
| 53 |
|
| 54 |
if ( $printed ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
$printed = true; |
| 59 |
?> |
| 60 |
<style> |
| 61 |
.tpt-visibility-roles__list { display: inline-flex; flex-direction: column; gap: 4px; vertical-align: top; } |
| 62 |
.tpt-visibility-roles__item { display: block !important; margin: 0 !important; float: none !important; width: auto !important; font-weight: normal; } |
| 63 |
.tpt-visibility-roles__item input { margin: 0 6px 0 0 !important; } |
| 64 |
</style> |
| 65 |
<script> |
| 66 |
( function () { |
| 67 |
function sync( list ) { |
| 68 |
var select = document.getElementById( list.dataset.modeField ); |
| 69 |
if ( ! select ) { return; } |
| 70 |
var namesRoles = select.value === <?php echo wp_json_encode( VisibilityRule::MODE_INCLUDE ); ?> || select.value === <?php echo wp_json_encode( VisibilityRule::MODE_EXCLUDE ); ?>; |
| 71 |
list.style.display = namesRoles ? '' : 'none'; |
| 72 |
} |
| 73 |
document.querySelectorAll( '.tpt-visibility-roles' ).forEach( function ( list ) { |
| 74 |
var select = document.getElementById( list.dataset.modeField ); |
| 75 |
if ( select ) { select.addEventListener( 'change', function () { sync( list ); } ); } |
| 76 |
sync( list ); |
| 77 |
} ); |
| 78 |
} )(); |
| 79 |
</script> |
| 80 |
<?php |
| 81 |
} |
| 82 |
} |
| 83 |
|