Simplify parse_attributes_field

This commit is contained in:
Mike Jolley 2019-06-17 13:52:14 +01:00
parent 2b3c870884
commit 836ece76b1
1 changed files with 25 additions and 47 deletions

View File

@ -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;
}