Introduce get_tax_class_slugs.

This commit is contained in:
Justin Shreve 2017-01-25 13:38:13 -08:00
parent e8237eab3d
commit 474a307420
8 changed files with 25 additions and 15 deletions

View File

@ -1038,11 +1038,10 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
// Inherit tax class from items
if ( 'inherit' === $shipping_tax_class ) {
$tax_rates = array();
$tax_classes = array_merge( array( '' ), WC_Tax::get_tax_classes() );
$tax_classes = array_merge( array( '' ), WC_Tax::get_tax_class_slugs() );
$found_tax_classes = $this->get_items_tax_classes();
foreach ( $tax_classes as $tax_class ) {
$tax_class = sanitize_title( $tax_class );
if ( in_array( $tax_class, $found_tax_classes ) ) {
$tax_rates = WC_Tax::find_shipping_rates( array(
'country' => $args['country'],

View File

@ -81,9 +81,9 @@ class WC_Settings_Tax extends WC_Settings_Page {
public function output() {
global $current_section;
$tax_classes = WC_Tax::get_tax_classes();
$tax_classes = WC_Tax::get_tax_class_slugs();
if ( 'standard' === $current_section || in_array( $current_section, array_map( 'sanitize_title', $tax_classes ) ) ) {
if ( 'standard' === $current_section || in_array( $current_section, $tax_classes ) ) {
$this->output_tax_rates();
} else {
$settings = $this->get_settings();

View File

@ -2622,7 +2622,7 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
$params['tax_class'] = array(
'description' => __( 'Limit result set to products with a specific tax class.', 'woocommerce' ),
'type' => 'string',
'enum' => array_map( 'sanitize_title', array_merge( array( 'standard' ), WC_Tax::get_tax_classes() ) ),
'enum' => array_merge( array( 'standard' ), WC_Tax::get_tax_class_slugs() ),
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => 'rest_validate_request_arg',
);

View File

@ -626,7 +626,7 @@ class WC_REST_Taxes_Controller extends WC_REST_Controller {
'description' => __( 'Tax class.', 'woocommerce' ),
'type' => 'string',
'default' => 'standard',
'enum' => array_merge( array( 'standard' ), array_map( 'sanitize_title', WC_Tax::get_tax_classes() ) ),
'enum' => array_merge( array( 'standard' ), WC_Tax::get_tax_class_slugs() ),
'context' => array( 'view', 'edit' ),
),
),
@ -684,7 +684,7 @@ class WC_REST_Taxes_Controller extends WC_REST_Controller {
);
$params['class'] = array(
'description' => __( 'Sort by tax class.', 'woocommerce' ),
'enum' => array_merge( array( 'standard' ), array_map( 'sanitize_title', WC_Tax::get_tax_classes() ) ),
'enum' => array_merge( array( 'standard' ), WC_Tax::get_tax_class_slugs() ),
'sanitize_callback' => 'sanitize_title',
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',

View File

@ -41,7 +41,7 @@ class WC_Order_Item_Fee extends WC_Order_Item {
* @throws WC_Data_Exception
*/
public function set_tax_class( $value ) {
if ( $value && ! in_array( $value, WC_Tax::get_tax_classes() ) ) {
if ( $value && ! in_array( $value, WC_Tax::get_tax_class_slugs() ) ) {
$this->error( 'order_item_fee_invalid_tax_class', __( 'Invalid tax class', 'woocommerce' ) );
}
$this->set_prop( 'tax_class', $value );

View File

@ -57,7 +57,7 @@ class WC_Order_Item_Product extends WC_Order_Item {
* @throws WC_Data_Exception
*/
public function set_tax_class( $value ) {
if ( $value && ! in_array( $value, WC_Tax::get_tax_classes() ) ) {
if ( $value && ! in_array( $value, WC_Tax::get_tax_class_slugs() ) ) {
$this->error( 'order_item_product_invalid_tax_class', __( 'Invalid tax class', 'woocommerce' ) );
}
$this->set_prop( 'tax_class', $value );

View File

@ -550,10 +550,9 @@ class WC_Tax {
// If multiple classes are found, use the first one found unless a standard rate item is found. This will be the first listed in the 'additonal tax class' section.
if ( sizeof( $cart_tax_classes ) > 1 && ! in_array( '', $cart_tax_classes ) ) {
$tax_classes = self::get_tax_classes();
$tax_classes = self::get_tax_class_slugs();
foreach ( $tax_classes as $tax_class ) {
$tax_class = sanitize_title( $tax_class );
if ( in_array( $tax_class, $cart_tax_classes ) ) {
$matched_tax_rates = self::find_shipping_rates( array(
'country' => $country,
@ -697,6 +696,16 @@ class WC_Tax {
return array_filter( array_map( 'trim', explode( "\n", get_option( 'woocommerce_tax_classes' ) ) ) );
}
/**
* Get store tax classes as slugs.
*
* @since 2.7.0
* @return array
*/
public static function get_tax_class_slugs() {
return array_map( 'sanitize_title', self::get_tax_classes() );
}
/**
* format the city.
* @param string $city
@ -759,9 +768,9 @@ class WC_Tax {
* @return string
*/
public static function format_tax_rate_class( $class ) {
$class = sanitize_title( $class );
$sanitized_classes = array_map( 'sanitize_title', self::get_tax_classes() );
if ( ! in_array( $class, $sanitized_classes ) ) {
$class = sanitize_title( $class );
$classes = self::get_tax_class_slugs();
if ( ! in_array( $class, $classes ) ) {
$class = '';
}
return ( 'standard' === $class ) ? '' : $class;

View File

@ -442,8 +442,10 @@ class WC_Tests_Tax extends WC_Unit_Test_Case {
*/
public function test_get_tax_classes() {
$tax_classes = WC_Tax::get_tax_classes();
$this->assertEquals( $tax_classes, array( 'Reduced rate', 'Zero rate' ) );
$tax_classes = WC_Tax::get_tax_class_slugs();
$this->assertEquals( $tax_classes, array( 'reduced-rate', 'zero-rate' ) );
}
/**