| 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 users or user roles that the rule will apply to.', 'tier-pricing-table' ); |
| 20 |
} |
| 21 |
|
| 22 |
public function render( GlobalPricingRule $pricingRule ) { |
| 23 |
|
| 24 |
$this->renderSectionTitle( __( 'Included Users', 'tier-pricing-table' ), array( |
| 25 |
'description' => __( 'Select users or user roles that the rule will apply to. The rule will work for all users with the selected roles.', |
| 26 |
'tier-pricing-table' ), |
| 27 |
) ); |
| 28 |
|
| 29 |
if ( empty( $pricingRule->getIncludedUserRoles() ) && empty( $pricingRule->getIncludedUsers() ) ) { |
| 30 |
$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).', |
| 31 |
'tier-pricing-table' ) ); |
| 32 |
} |
| 33 |
|
| 34 |
$this->renderSelect2( array( |
| 35 |
'id' => 'tpt_included_user_roles', |
| 36 |
'label' => __( 'Include user roles', 'tier-pricing-table' ), |
| 37 |
'options' => ( function () { |
| 38 |
return array_map( function ( $WPRole ) { |
| 39 |
return $WPRole['name']; |
| 40 |
}, wp_roles()->roles ); |
| 41 |
} )(), |
| 42 |
'value' => $pricingRule->getIncludedUserRoles(), |
| 43 |
'placeholder' => __( 'Select for a user role', 'tier-pricing-table' ), |
| 44 |
'search_action' => 'woocommerce_json_search_tpt_user_roles', |
| 45 |
'css_class' => 'tpt-select-woo', |
| 46 |
'desc_tip' => false, |
| 47 |
'description' => function () { |
| 48 |
$settingsLink = ServiceContainer::getInstance()->getSettings()->getLink(); |
| 49 |
|
| 50 |
$settingsLink = add_query_arg( [ |
| 51 |
'section' => 'tools', |
| 52 |
], $settingsLink ); |
| 53 |
|
| 54 |
$settingsLink .= '#roles'; |
| 55 |
|
| 56 |
?> |
| 57 |
<a href="<?php echo esc_url( $settingsLink ); ?>" |
| 58 |
target="_blank"><?php esc_html_e( 'Manage user roles', 'tier-pricing-table' ); ?></a> |
| 59 |
<?php |
| 60 |
}, |
| 61 |
) ); |
| 62 |
|
| 63 |
|
| 64 |
$this->renderSelect2( array( |
| 65 |
'id' => 'tpt_included_users', |
| 66 |
'label' => __( 'Include specific customers', 'tier-pricing-table' ), |
| 67 |
'options' => ( function () use ( $pricingRule ) { |
| 68 |
$users = []; |
| 69 |
foreach ( $pricingRule->getIncludedUsers() as $userId ) { |
| 70 |
$customer = new \WC_Customer( $userId ); |
| 71 |
|
| 72 |
if ( $customer->get_id() ) { |
| 73 |
$users[ $userId ] = Formatter::formatCustomerString( $customer ); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
return $users; |
| 78 |
} )(), |
| 79 |
'value' => $pricingRule->getIncludedUsers(), |
| 80 |
'placeholder' => __( 'Select for a customer', 'tier-pricing-table' ), |
| 81 |
'search_action' => 'woocommerce_json_search_tpt_customers', |
| 82 |
'css_class' => 'rbp-select-woo wc-product-search', |
| 83 |
) ); |
| 84 |
|
| 85 |
$this->renderSectionTitle( __( 'Exclusions', 'tier-pricing-table' ), array( |
| 86 |
'description' => __( 'Select users or user roles the rule will not work for.', 'tier-pricing-table' ), |
| 87 |
) ); |
| 88 |
|
| 89 |
$this->renderSelect2( array( |
| 90 |
'id' => 'tpt_excluded_user_roles', |
| 91 |
'label' => __( 'Exclude user roles', 'tier-pricing-table' ), |
| 92 |
'options' => ( function () { |
| 93 |
$roles = []; |
| 94 |
foreach ( wp_roles()->roles as $key => $WPRole ) { |
| 95 |
$roles[ $key ] = $WPRole['name']; |
| 96 |
} |
| 97 |
|
| 98 |
return $roles; |
| 99 |
} )(), |
| 100 |
'value' => $pricingRule->getExcludedUserRoles(), |
| 101 |
'placeholder' => __( 'Select for a user role', 'tier-pricing-table' ), |
| 102 |
'search_action' => 'woocommerce_json_search_tpt_user_roles', |
| 103 |
'css_class' => 'tpt-select-woo', |
| 104 |
) ); |
| 105 |
|
| 106 |
$this->renderSelect2( array( |
| 107 |
'id' => 'tpt_excluded_users', |
| 108 |
'label' => __( 'Exclude specific customers', 'tier-pricing-table' ), |
| 109 |
'options' => ( function () use ( $pricingRule ) { |
| 110 |
$users = []; |
| 111 |
foreach ( $pricingRule->getExcludedUsers() as $userId ) { |
| 112 |
$customer = new \WC_Customer( $userId ); |
| 113 |
|
| 114 |
if ( $customer->get_id() ) { |
| 115 |
$users[ $userId ] = Formatter::formatCustomerString( $customer ); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
return $users; |
| 120 |
} )(), |
| 121 |
'value' => $pricingRule->getExcludedUsers(), |
| 122 |
'placeholder' => __( 'Select for a customer', 'tier-pricing-table' ), |
| 123 |
'search_action' => 'woocommerce_json_search_tpt_customers', |
| 124 |
'css_class' => 'rbp-select-woo wc-product-search', |
| 125 |
) ); |
| 126 |
} |
| 127 |
|
| 128 |
public function getIcon(): string { |
| 129 |
return 'dashicons-admin-users'; |
| 130 |
} |
| 131 |
} |
| 132 |
|