| 1 |
<?php namespace TierPricingTable\Addons\TaxSettings\Settings; |
| 2 |
|
| 3 |
class Settings { |
| 4 |
|
| 5 |
public function __construct() { |
| 6 |
|
| 7 |
add_filter( 'woocommerce_get_settings_tax', function ( $settings, $current_section ) { |
| 8 |
if ( '' === $current_section ) { |
| 9 |
$settings[] = array( |
| 10 |
'type' => 'tiered-pricing_tax-ui', |
| 11 |
); |
| 12 |
} |
| 13 |
|
| 14 |
return $settings; |
| 15 |
}, 10, 2 ); |
| 16 |
|
| 17 |
add_action( 'woocommerce_settings_save_tax', function () { |
| 18 |
if ( isset( $_POST['tpt_role_tax_settings'] ) ) { |
| 19 |
$params = json_decode( stripslashes( $_POST['tpt_role_tax_settings'] ), true ); |
| 20 |
|
| 21 |
$sanitized_settings = array(); |
| 22 |
|
| 23 |
if ( is_array( $params ) ) { |
| 24 |
foreach ( $params as $role => $data ) { |
| 25 |
if ( is_array( $data ) ) { |
| 26 |
$sanitized_settings[ sanitize_key( $role ) ] = array( |
| 27 |
'tax_exempt' => isset( $data['tax_exempt'] ) ? (bool) $data['tax_exempt'] : false, |
| 28 |
'tax_class' => isset( $data['tax_class'] ) ? sanitize_text_field( $data['tax_class'] ) : 'default', |
| 29 |
'display_shop' => isset( $data['display_shop'] ) ? sanitize_text_field( $data['display_shop'] ) : 'default', |
| 30 |
'display_cart' => isset( $data['display_cart'] ) ? sanitize_text_field( $data['display_cart'] ) : 'default', |
| 31 |
'prices_include_tax' => isset( $data['prices_include_tax'] ) ? sanitize_text_field( $data['prices_include_tax'] ) : 'default', |
| 32 |
'price_suffix' => isset( $data['price_suffix'] ) ? sanitize_text_field( $data['price_suffix'] ) : '', |
| 33 |
); |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
update_option( '_tpt_role_tax_settings', $sanitized_settings ); |
| 38 |
} |
| 39 |
} ); |
| 40 |
|
| 41 |
add_action( 'woocommerce_admin_field_tiered-pricing_tax-ui', function () { |
| 42 |
?> |
| 43 |
<tr valign="top"> |
| 44 |
<td colspan="2" class="forminp"> |
| 45 |
<div id="tiered-pricing__feature__tax"></div> |
| 46 |
</td> |
| 47 |
</tr> |
| 48 |
<?php |
| 49 |
} ); |
| 50 |
|
| 51 |
add_action( 'admin_enqueue_scripts', function () { |
| 52 |
|
| 53 |
$page = isset( $_GET['page'] ) ? sanitize_text_field( $_GET['page'] ) : ''; |
| 54 |
$settingsTab = isset( $_GET['tab'] ) ? sanitize_text_field( $_GET['tab'] ) : ''; |
| 55 |
$section = isset( $_GET['section'] ) ? sanitize_text_field( $_GET['section'] ) : ''; |
| 56 |
|
| 57 |
if ( 'wc-settings' !== $page || 'tax' !== $settingsTab || '' !== $section ) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
if ( ! file_exists( plugin_dir_path( __FILE__ ) . '../build/index.asset.php' ) ) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
$assetFile = include( plugin_dir_path( __FILE__ ) . '../build/index.asset.php' ); |
| 66 |
|
| 67 |
wp_enqueue_script( 'tiered-pricing/feature/tax', |
| 68 |
plugins_url( 'build/index.js', dirname( __FILE__ . '../' ) ), $assetFile['dependencies'], |
| 69 |
$assetFile['version'], true ); |
| 70 |
|
| 71 |
wp_set_script_translations( 'tiered-pricing/feature/tax', 'tier-pricing-table', |
| 72 |
dirname( __FILE__, 5 ) . '/languages' ); |
| 73 |
|
| 74 |
$roles = wp_roles()->get_names(); |
| 75 |
$formattedRoles = array(); |
| 76 |
foreach ( $roles as $value => $label ) { |
| 77 |
$formattedRoles[] = array( 'value' => $value, 'label' => translate_user_role( $label ) ); |
| 78 |
} |
| 79 |
|
| 80 |
wp_localize_script( 'tiered-pricing/feature/tax', 'tptTaxSettings', array( |
| 81 |
'roles' => $formattedRoles, |
| 82 |
'isPremium' => function_exists( 'tpt_fs' ) && tpt_fs()->can_use_premium_code(), |
| 83 |
'upgradeUrl' => function_exists( 'tpt_fs' ) ? tpt_fs()->get_upgrade_url() : '#', |
| 84 |
) ); |
| 85 |
|
| 86 |
wp_enqueue_style( 'wp-components' ); |
| 87 |
} ); |
| 88 |
} |
| 89 |
} |
| 90 |
|