From 836ece76b14562253fc7cddefa61c6a2859bdd1d Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 17 Jun 2019 13:52:14 +0100 Subject: [PATCH] Simplify parse_attributes_field --- .../Version4/Schema/ProductRequest.php | 72 +++++++------------ 1 file changed, 25 insertions(+), 47 deletions(-) diff --git a/src/Controllers/Version4/Schema/ProductRequest.php b/src/Controllers/Version4/Schema/ProductRequest.php index 6291b572518..8bc30c3aa18 100644 --- a/src/Controllers/Version4/Schema/ProductRequest.php +++ b/src/Controllers/Version4/Schema/ProductRequest.php @@ -55,8 +55,13 @@ class ProductRequest extends AbstractRequest { $type = $this->get_param( 'type', '' ); if ( $type ) { - $classname = '\\' . \WC_Product_Factory::get_classname_from_product_type( $type ); - $object = class_exists( $classname ) ? new $classname( $id ) : new \WC_Product_Simple( $id ); + $classname = \WC_Product_Factory::get_classname_from_product_type( $type ); + if ( $classname && class_exists( '\\' . $classname ) ) { + $classname = '\\' . $classname; + } else { + $classname = '\WC_Product_Simple'; + } + $object = new $classname( $id ); } elseif ( $id ) { $object = wc_get_product( $id ); } else { @@ -256,63 +261,36 @@ class ProductRequest extends AbstractRequest { $attributes = array(); foreach ( $raw_attributes as $attribute ) { - $attribute_id = 0; - $attribute_name = ''; - // Check ID for global attributes or name for product attributes. if ( ! empty( $attribute['id'] ) ) { $attribute_id = absint( $attribute['id'] ); $attribute_name = wc_attribute_taxonomy_name_by_id( $attribute_id ); } elseif ( ! empty( $attribute['name'] ) ) { + $attribute_id = 0; $attribute_name = wc_clean( $attribute['name'] ); } - if ( ! $attribute_id && ! $attribute_name ) { + if ( ! $attribute_name || ! isset( $attribute['options'] ) ) { continue; } - if ( $attribute_id ) { - - if ( isset( $attribute['options'] ) ) { - $options = $attribute['options']; - - if ( ! is_array( $attribute['options'] ) ) { - // Text based attributes - Posted values are term names. - $options = explode( WC_DELIMITER, $options ); - } - - $values = array_map( 'wc_sanitize_term_text_based', $options ); - $values = array_filter( $values, 'strlen' ); - } else { - $values = array(); - } - - if ( ! empty( $values ) ) { - // Add attribute to array, but don't set values. - $attribute_object = new \WC_Product_Attribute(); - $attribute_object->set_id( $attribute_id ); - $attribute_object->set_name( $attribute_name ); - $attribute_object->set_options( $values ); - $attribute_object->set_position( isset( $attribute['position'] ) ? (string) absint( $attribute['position'] ) : '0' ); - $attribute_object->set_visible( ( isset( $attribute['visible'] ) && $attribute['visible'] ) ? 1 : 0 ); - $attribute_object->set_variation( ( isset( $attribute['variation'] ) && $attribute['variation'] ) ? 1 : 0 ); - $attributes[] = $attribute_object; - } - } elseif ( isset( $attribute['options'] ) ) { - // Custom attribute - Add attribute to array and set the values. - if ( is_array( $attribute['options'] ) ) { - $values = $attribute['options']; - } else { - $values = explode( WC_DELIMITER, $attribute['options'] ); - } - $attribute_object = new \WC_Product_Attribute(); - $attribute_object->set_name( $attribute_name ); - $attribute_object->set_options( $values ); - $attribute_object->set_position( isset( $attribute['position'] ) ? (string) absint( $attribute['position'] ) : '0' ); - $attribute_object->set_visible( ( isset( $attribute['visible'] ) && $attribute['visible'] ) ? 1 : 0 ); - $attribute_object->set_variation( ( isset( $attribute['variation'] ) && $attribute['variation'] ) ? 1 : 0 ); - $attributes[] = $attribute_object; + if ( ! is_array( $attribute['options'] ) ) { + // Text based attributes - Posted values are term names. + $attribute['options'] = explode( \WC_DELIMITER, $attribute['options'] ); } + + if ( $attribute_id ) { + $attribute['options'] = array_filter( array_map( 'wc_sanitize_term_text_based', $attribute['options'] ), 'strlen' ); + } + + $attribute_object = new \WC_Product_Attribute(); + $attribute_object->set_id( $attribute_id ); + $attribute_object->set_name( $attribute_name ); + $attribute_object->set_options( $attribute['options'] ); + $attribute_object->set_position( isset( $attribute['position'] ) ? (string) absint( $attribute['position'] ) : '0' ); + $attribute_object->set_visible( ! empty( $attribute['visible'] ) ? 1 : 0 ); + $attribute_object->set_variation( ! empty( $attribute['variation'] ) ? 1 : 0 ); + $attributes[] = $attribute_object; } return $attributes; }