tier-pricing-table
/
src
/
Addons
/
NonLoggedInUsers
/
Visibility
/
Admin
/
CategoryVisibilityFields.php
CategoryVisibilityFields.php in Tiered Pricing Table for WooCommerce 8.0.2, at src/Addons/NonLoggedInUsers/Visibility/Admin/CategoryVisibilityFields.php
| 1 | <?php namespace TierPricingTable\Addons\NonLoggedInUsers\Visibility\Admin; |
| 2 | |
| 3 | use TierPricingTable\Addons\NonLoggedInUsers\Visibility\VisibilityMeta; |
| 4 | use TierPricingTable\Addons\NonLoggedInUsers\Visibility\VisibilityRule; |
| 5 | use WP_Term; |
| 6 | |
| 7 | /** |
| 8 | * "Who can see this category" on the product category screens. |
| 9 | */ |
| 10 | class CategoryVisibilityFields { |
| 11 | |
| 12 | const NONCE = 'tpt_visibility_category'; |
| 13 | |
| 14 | public function __construct() { |
| 15 | add_action( 'product_cat_add_form_fields', array( $this, 'renderAdd' ) ); |
| 16 | add_action( 'product_cat_edit_form_fields', array( $this, 'renderEdit' ) ); |
| 17 | add_action( 'created_product_cat', array( $this, 'save' ) ); |
| 18 | add_action( 'edited_product_cat', array( $this, 'save' ) ); |
| 19 | } |
| 20 | |
| 21 | public function renderAdd() { |
| 22 | $rule = VisibilityRule::normalize( VisibilityRule::MODE_EVERYONE, array() ); |
| 23 | |
| 24 | wp_nonce_field( self::NONCE, self::NONCE ); |
| 25 | ?> |
| 26 | <div class="form-field"> |
| 27 | <label for="<?php echo esc_attr( VisibilityMeta::TERM_MODE ); ?>"><?php esc_html_e( 'Who can see this category', 'tier-pricing-table' ); ?></label> |
| 28 | <?php $this->modeSelect( $rule['mode'] ); ?> |
| 29 | <p><?php esc_html_e( 'Hidden categories and their products leave the shop, search and menus. Shop managers always see everything.', 'tier-pricing-table' ); ?></p> |
| 30 | </div> |
| 31 | <div class="form-field tpt-visibility-roles" data-mode-field="<?php echo esc_attr( VisibilityMeta::TERM_MODE ); ?>"> |
| 32 | <label><?php esc_html_e( 'Roles', 'tier-pricing-table' ); ?></label> |
| 33 | <?php foreach ( VisibilityMeta::getRoleOptions() as $role => $label ) : ?> |
| 34 | <label class="tpt-visibility-roles__item"><input type="checkbox" name="<?php echo esc_attr( VisibilityMeta::TERM_ROLES ); ?>[]" value="<?php echo esc_attr( $role ); ?>"> <?php echo esc_html( $label ); ?></label> |
| 35 | <?php endforeach; ?> |
| 36 | </div> |
| 37 | <?php |
| 38 | RoleChecklist::printAssets(); |
| 39 | } |
| 40 | |
| 41 | public function renderEdit( WP_Term $term ) { |
| 42 | $rule = VisibilityMeta::getCategoryRule( (int) $term->term_id ); |
| 43 | |
| 44 | wp_nonce_field( self::NONCE, self::NONCE ); |
| 45 | ?> |
| 46 | <tr class="form-field"> |
| 47 | <th scope="row"><label for="<?php echo esc_attr( VisibilityMeta::TERM_MODE ); ?>"><?php esc_html_e( 'Who can see this category', 'tier-pricing-table' ); ?></label></th> |
| 48 | <td> |
| 49 | <?php $this->modeSelect( $rule['mode'] ); ?> |
| 50 | <p class="description"><?php esc_html_e( 'Hidden categories and their products leave the shop, search and menus. Shop managers always see everything.', 'tier-pricing-table' ); ?></p> |
| 51 | </td> |
| 52 | </tr> |
| 53 | <?php |
| 54 | RoleChecklist::render( VisibilityMeta::TERM_ROLES, $rule['roles'], VisibilityMeta::TERM_MODE, true ); |
| 55 | } |
| 56 | |
| 57 | protected function modeSelect( string $current ) { |
| 58 | ?> |
| 59 | <select name="<?php echo esc_attr( VisibilityMeta::TERM_MODE ); ?>" id="<?php echo esc_attr( VisibilityMeta::TERM_MODE ); ?>"> |
| 60 | <?php foreach ( VisibilityMeta::getModeOptions( false ) as $mode => $label ) : ?> |
| 61 | <option value="<?php echo esc_attr( $mode ); ?>" <?php selected( $current, $mode ); ?>><?php echo esc_html( $label ); ?></option> |
| 62 | <?php endforeach; ?> |
| 63 | </select> |
| 64 | <?php |
| 65 | } |
| 66 | |
| 67 | public function save( $termId ) { |
| 68 | if ( ! isset( $_POST[ self::NONCE ] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[ self::NONCE ] ) ), self::NONCE ) ) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | if ( ! current_user_can( 'manage_product_terms' ) ) { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | $mode = isset( $_POST[ VisibilityMeta::TERM_MODE ] ) ? sanitize_key( wp_unslash( $_POST[ VisibilityMeta::TERM_MODE ] ) ) : VisibilityRule::MODE_EVERYONE; |
| 77 | $roles = isset( $_POST[ VisibilityMeta::TERM_ROLES ] ) ? array_map( 'sanitize_key', (array) wp_unslash( $_POST[ VisibilityMeta::TERM_ROLES ] ) ) : array(); |
| 78 | |
| 79 | VisibilityMeta::saveCategoryRule( (int) $termId, $mode, $roles ); |
| 80 | } |
| 81 | } |
| 82 |