WC_Settings_Tax

WC_Settings_Tax.

Defined (1)

The class is defined in the following location(s).

/includes/admin/settings/class-wc-settings-tax.php  
  1. class WC_Settings_Tax extends WC_Settings_Page { 
  2.  
  3. /** 
  4. * Setting page id. 
  5. * @var string 
  6. */ 
  7. protected $id = 'tax'; 
  8.  
  9. /** 
  10. * Constructor. 
  11. */ 
  12. public function __construct() { 
  13. $this->label = __( 'Tax', 'woocommerce' ); 
  14. parent::__construct(); 
  15.  
  16. /** 
  17. * Add this page to settings. 
  18. */ 
  19. public function add_settings_page( $pages ) { 
  20. if ( wc_tax_enabled() ) { 
  21. return parent::add_settings_page( $pages ); 
  22. } else { 
  23. return $pages; 
  24.  
  25. /** 
  26. * Get sections. 
  27. * @return array 
  28. */ 
  29. public function get_sections() { 
  30. $sections = array( 
  31. '' => __( 'Tax options', 'woocommerce' ),  
  32. 'standard' => __( 'Standard rates', 'woocommerce' ),  
  33. ); 
  34.  
  35. // Get tax classes and display as links 
  36. $tax_classes = WC_Tax::get_tax_classes(); 
  37.  
  38. foreach ( $tax_classes as $class ) { 
  39. $sections[ sanitize_title( $class ) ] = sprintf( __( '%s rates', 'woocommerce' ), $class ); 
  40.  
  41. return apply_filters( 'woocommerce_get_sections_' . $this->id, $sections ); 
  42.  
  43. /** 
  44. * Get settings array. 
  45. * @return array 
  46. */ 
  47. public function get_settings() { 
  48. return apply_filters( 'woocommerce_get_settings_' . $this->id, include( 'views/settings-tax.php' ) ); 
  49.  
  50. /** 
  51. * Output the settings. 
  52. */ 
  53. public function output() { 
  54. global $current_section; 
  55.  
  56. $tax_classes = WC_Tax::get_tax_class_slugs(); 
  57.  
  58. if ( 'standard' === $current_section || in_array( $current_section, $tax_classes ) ) { 
  59. $this->output_tax_rates(); 
  60. } else { 
  61. $settings = $this->get_settings(); 
  62.  
  63. WC_Admin_Settings::output_fields( $settings ); 
  64.  
  65. /** 
  66. * Save settings. 
  67. */ 
  68. public function save() { 
  69. global $current_section, $wpdb; 
  70.  
  71. if ( ! $current_section ) { 
  72. $settings = $this->get_settings(); 
  73. WC_Admin_Settings::save_fields( $settings ); 
  74.  
  75. } elseif ( ! empty( $_POST['tax_rate_country'] ) ) { 
  76. $this->save_tax_rates(); 
  77.  
  78. WC_Cache_Helper::incr_cache_prefix( 'taxes' ); 
  79.  
  80. /** 
  81. * Output tax rate tables. 
  82. */ 
  83. public function output_tax_rates() { 
  84. global $wpdb, $current_section; 
  85.  
  86. $current_class = $this->get_current_tax_class(); 
  87.  
  88. $countries = array(); 
  89. foreach ( WC()->countries->get_allowed_countries() as $value => $label ) { 
  90. $countries[] = array( 
  91. 'value' => $value,  
  92. 'label' => esc_js( html_entity_decode( $label ) ),  
  93. ); 
  94.  
  95. $states = array(); 
  96. foreach ( WC()->countries->get_allowed_country_states() as $label ) { 
  97. foreach ( $label as $code => $state ) { 
  98. $states[] = array( 
  99. 'value' => $code,  
  100. 'label' => esc_js( html_entity_decode( $state ) ),  
  101. ); 
  102.  
  103. $base_url = admin_url( add_query_arg( array( 
  104. 'page' => 'wc-settings',  
  105. 'tab' => 'tax',  
  106. 'section' => $current_section,  
  107. ), 'admin.php' ) ); 
  108.  
  109. // Localize and enqueue our js. 
  110. wp_localize_script( 'wc-settings-tax', 'htmlSettingsTaxLocalizeScript', array( 
  111. 'current_class' => $current_class,  
  112. 'wc_tax_nonce' => wp_create_nonce( 'wc_tax_nonce-class:' . $current_class ),  
  113. 'base_url' => $base_url,  
  114. 'rates' => array_values( WC_Tax::get_rates_for_tax_class( $current_class ) ),  
  115. 'page' => ! empty( $_GET['p'] ) ? absint( $_GET['p'] ) : 1,  
  116. 'limit' => 100,  
  117. 'countries' => $countries,  
  118. 'states' => $states,  
  119. 'default_rate' => array( 
  120. 'tax_rate_id' => 0,  
  121. 'tax_rate_country' => '',  
  122. 'tax_rate_state' => '',  
  123. 'tax_rate' => '',  
  124. 'tax_rate_name' => '',  
  125. 'tax_rate_priority' => 1,  
  126. 'tax_rate_compound' => 0,  
  127. 'tax_rate_shipping' => 1,  
  128. 'tax_rate_order' => null,  
  129. 'tax_rate_class' => $current_class,  
  130. ),  
  131. 'strings' => array( 
  132. 'no_rows_selected' => __( 'No row(s) selected', 'woocommerce' ),  
  133. 'unload_confirmation_msg' => __( 'Your changed data will be lost if you leave this page without saving.', 'woocommerce' ),  
  134. 'csv_data_cols' => array( 
  135. __( 'Country code', 'woocommerce' ),  
  136. __( 'State code', 'woocommerce' ),  
  137. __( 'Postcode / ZIP', 'woocommerce' ),  
  138. __( 'City', 'woocommerce' ),  
  139. __( 'Rate %', 'woocommerce' ),  
  140. __( 'Tax name', 'woocommerce' ),  
  141. __( 'Priority', 'woocommerce' ),  
  142. __( 'Compound', 'woocommerce' ),  
  143. __( 'Shipping', 'woocommerce' ),  
  144. __( 'Tax class', 'woocommerce' ),  
  145. ),  
  146. ),  
  147. ) ); 
  148. wp_enqueue_script( 'wc-settings-tax' ); 
  149.  
  150. include( 'views/html-settings-tax.php' ); 
  151.  
  152. /** 
  153. * Get tax class being edited. 
  154. * @return string 
  155. */ 
  156. private static function get_current_tax_class() { 
  157. global $current_section; 
  158.  
  159. $tax_classes = WC_Tax::get_tax_classes(); 
  160. $current_class = ''; 
  161.  
  162. foreach ( $tax_classes as $class ) { 
  163. if ( sanitize_title( $class ) == $current_section ) { 
  164. $current_class = $class; 
  165.  
  166. return $current_class; 
  167.  
  168. /** 
  169. * Get a posted tax rate. 
  170. * @param string $key Key of tax rate in the post data array 
  171. * @param int $order Position/order of rate 
  172. * @param string $class Tax class for rate 
  173. * @return array 
  174. */ 
  175. private function get_posted_tax_rate( $key, $order, $class ) { 
  176. $tax_rate = array(); 
  177. $tax_rate_keys = array( 
  178. 'tax_rate_country',  
  179. 'tax_rate_state',  
  180. 'tax_rate',  
  181. 'tax_rate_name',  
  182. 'tax_rate_priority',  
  183. ); 
  184.  
  185. foreach ( $tax_rate_keys as $tax_rate_key ) { 
  186. if ( isset( $_POST[ $tax_rate_key ] ) && isset( $_POST[ $tax_rate_key ][ $key ] ) ) { 
  187. $tax_rate[ $tax_rate_key ] = wc_clean( $_POST[ $tax_rate_key ][ $key ] ); 
  188.  
  189. $tax_rate['tax_rate_compound'] = isset( $_POST['tax_rate_compound'][ $key ] ) ? 1 : 0; 
  190. $tax_rate['tax_rate_shipping'] = isset( $_POST['tax_rate_shipping'][ $key ] ) ? 1 : 0; 
  191. $tax_rate['tax_rate_order'] = $order; 
  192. $tax_rate['tax_rate_class'] = $class; 
  193.  
  194. return $tax_rate; 
  195.  
  196. /** 
  197. * Save tax rates. 
  198. */ 
  199. public function save_tax_rates() { 
  200. global $wpdb; 
  201.  
  202. $current_class = sanitize_title( $this->get_current_tax_class() ); 
  203.  
  204. // get the tax rate id of the first submited row 
  205. $first_tax_rate_id = key( $_POST['tax_rate_country'] ); 
  206.  
  207. // get the order position of the first tax rate id 
  208. $tax_rate_order = absint( $wpdb->get_var( $wpdb->prepare( "SELECT tax_rate_order FROM {$wpdb->prefix}woocommerce_tax_rates WHERE tax_rate_id = %s", $first_tax_rate_id ) ) ); 
  209.  
  210. $index = isset( $tax_rate_order ) ? $tax_rate_order : 0; 
  211.  
  212. // Loop posted fields 
  213. foreach ( $_POST['tax_rate_country'] as $key => $value ) { 
  214. $mode = ( 0 === strpos( $key, 'new-' ) ) ? 'insert' : 'update'; 
  215. $tax_rate = $this->get_posted_tax_rate( $key, $index ++, $current_class ); 
  216.  
  217. if ( 'insert' === $mode ) { 
  218. $tax_rate_id = WC_Tax::_insert_tax_rate( $tax_rate ); 
  219. } elseif ( 1 == $_POST['remove_tax_rate'][ $key ] ) { 
  220. $tax_rate_id = absint( $key ); 
  221. WC_Tax::_delete_tax_rate( $tax_rate_id ); 
  222. continue; 
  223. } else { 
  224. $tax_rate_id = absint( $key ); 
  225. WC_Tax::_update_tax_rate( $tax_rate_id, $tax_rate ); 
  226.  
  227. if ( isset( $_POST['tax_rate_postcode'][ $key ] ) ) { 
  228. WC_Tax::_update_tax_rate_postcodes( $tax_rate_id, wc_clean( $_POST['tax_rate_postcode'][ $key ] ) ); 
  229. if ( isset( $_POST['tax_rate_city'][ $key ] ) ) { 
  230. WC_Tax::_update_tax_rate_cities( $tax_rate_id, wc_clean( $_POST['tax_rate_city'][ $key ] ) );