| 1 |
<?php namespace TierPricingTable\Addons\GlobalTieredPricing\CPT\Form\Tabs; |
| 2 |
|
| 3 |
use TierPricingTable\Addons\GlobalTieredPricing\CPT\Form\FormTab; |
| 4 |
use TierPricingTable\Addons\GlobalTieredPricing\Formatter; |
| 5 |
use TierPricingTable\Addons\GlobalTieredPricing\GlobalPricingRule; |
| 6 |
use TierPricingTable\Core\ServiceContainer; |
| 7 |
|
| 8 |
class UsersAndRoles extends FormTab { |
| 9 |
|
| 10 |
public function getId(): string { |
| 11 |
return 'user-and-roles'; |
| 12 |
} |
| 13 |
|
| 14 |
public function getTitle(): string { |
| 15 |
return __( 'Users & Roles', 'tier-pricing-table' ); |
| 16 |
} |
| 17 |
|
| 18 |
public function getDescription(): string { |
| 19 |
return __( 'Select the users or roles this rule applies to', 'tier-pricing-table' ); |
| 20 |
} |
| 21 |
|
| 22 |
public function render( GlobalPricingRule $pricingRule ) { |
| 23 |
|
| 24 |
$this->renderSectionTitle( __( 'Applies to users', 'tier-pricing-table' ) ); |
| 25 |
|
| 26 |
if ( empty( $pricingRule->getIncludedUserRoles() ) && empty( $pricingRule->getIncludedUsers() ) ) { |
| 27 |
$this->renderHint( __( 'The rule will apply to all users if you do not specify user roles or specific customers (excluding those selected in the exclusions section).', |
| 28 |
'tier-pricing-table' ) ); |
| 29 |
} |
| 30 |
|
| 31 |
$this->renderSelect2( array( |
| 32 |
'id' => 'tpt_included_user_roles', |
| 33 |
'label' => __( 'User roles', 'tier-pricing-table' ), |
| 34 |
'options' => ( function () { |
| 35 |
return array_map( function ( $WPRole ) { |
| 36 |
return $WPRole['name']; |
| 37 |
}, wp_roles()->roles ); |
| 38 |
} )(), |
| 39 |
'value' => $pricingRule->getIncludedUserRoles(), |
| 40 |
'placeholder' => __( 'Select for a user role', 'tier-pricing-table' ), |
| 41 |
'search_action' => 'woocommerce_json_search_tpt_user_roles', |
| 42 |
'css_class' => 'tpt-select-woo', |
| 43 |
'desc_tip' => false, |
| 44 |
'description' => function () { |
| 45 |
$settingsLink = ServiceContainer::getInstance()->getSettings()->getLink(); |
| 46 |
|
| 47 |
$settingsLink = add_query_arg( [ |
| 48 |
'section' => 'advanced', |
| 49 |
], $settingsLink ); |
| 50 |
|
| 51 |
$settingsLink .= '#roles'; |
| 52 |
|
| 53 |
?> |
| 54 |
<a href="<?php echo esc_url( $settingsLink ); ?>" |
| 55 |
target="_blank"><?php esc_html_e( 'Manage user roles', 'tier-pricing-table' ); ?></a> |
| 56 |
<?php |
| 57 |
}, |
| 58 |
) ); |
| 59 |
|
| 60 |
|
| 61 |
$this->renderSelect2( array( |
| 62 |
'id' => 'tpt_included_users', |
| 63 |
'label' => __( 'Customers', 'tier-pricing-table' ), |
| 64 |
'options' => ( function () use ( $pricingRule ) { |
| 65 |
$users = []; |
| 66 |
foreach ( $pricingRule->getIncludedUsers() as $userId ) { |
| 67 |
$customer = new \WC_Customer( $userId ); |
| 68 |
|
| 69 |
if ( $customer->get_id() ) { |
| 70 |
$users[ $userId ] = Formatter::formatCustomerString( $customer ); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return $users; |
| 75 |
} )(), |
| 76 |
'value' => $pricingRule->getIncludedUsers(), |
| 77 |
'placeholder' => __( 'Select for a customer', 'tier-pricing-table' ), |
| 78 |
'search_action' => 'woocommerce_json_search_tpt_customers', |
| 79 |
'css_class' => 'rbp-select-woo wc-product-search', |
| 80 |
) ); |
| 81 |
|
| 82 |
$hasExclusions = ! empty( $pricingRule->getExcludedUserRoles() ) || |
| 83 |
! empty( $pricingRule->getExcludedUsers() ); |
| 84 |
?> |
| 85 |
<details class="tpt-exclusions-accordion" <?php echo $hasExclusions ? 'open' : ''; ?>> |
| 86 |
<summary> |
| 87 |
<?php echo esc_html__( 'Exclusions', 'tier-pricing-table' ); ?> |
| 88 |
<span class="dashicons dashicons-arrow-down-alt2 tpt-accordion-icon"></span> |
| 89 |
</summary> |
| 90 |
<div class="tpt-exclusions-accordion-content"> |
| 91 |
<?php |
| 92 |
|
| 93 |
$this->renderSelect2( array( |
| 94 |
'id' => 'tpt_excluded_user_roles', |
| 95 |
'label' => __( 'User roles', 'tier-pricing-table' ), |
| 96 |
'options' => ( function () { |
| 97 |
$roles = []; |
| 98 |
foreach ( wp_roles()->roles as $key => $WPRole ) { |
| 99 |
$roles[ $key ] = $WPRole['name']; |
| 100 |
} |
| 101 |
|
| 102 |
return $roles; |
| 103 |
} )(), |
| 104 |
'value' => $pricingRule->getExcludedUserRoles(), |
| 105 |
'placeholder' => __( 'Select for a user role', 'tier-pricing-table' ), |
| 106 |
'search_action' => 'woocommerce_json_search_tpt_user_roles', |
| 107 |
'css_class' => 'tpt-select-woo', |
| 108 |
) ); |
| 109 |
|
| 110 |
$this->renderSelect2( array( |
| 111 |
'id' => 'tpt_excluded_users', |
| 112 |
'label' => __( 'Customers', 'tier-pricing-table' ), |
| 113 |
'options' => ( function () use ( $pricingRule ) { |
| 114 |
$users = []; |
| 115 |
foreach ( $pricingRule->getExcludedUsers() as $userId ) { |
| 116 |
$customer = new \WC_Customer( $userId ); |
| 117 |
|
| 118 |
if ( $customer->get_id() ) { |
| 119 |
$users[ $userId ] = Formatter::formatCustomerString( $customer ); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
return $users; |
| 124 |
} )(), |
| 125 |
'value' => $pricingRule->getExcludedUsers(), |
| 126 |
'placeholder' => __( 'Select for a customer', 'tier-pricing-table' ), |
| 127 |
'search_action' => 'woocommerce_json_search_tpt_customers', |
| 128 |
'css_class' => 'rbp-select-woo wc-product-search', |
| 129 |
) ); |
| 130 |
|
| 131 |
?> |
| 132 |
</div> |
| 133 |
</details> |
| 134 |
<?php |
| 135 |
} |
| 136 |
|
| 137 |
public function getIcon(): string { |
| 138 |
return 'dashicons-admin-users'; |
| 139 |
} |
| 140 |
} |
| 141 |
|