Invert special importer mapping options to allow multiple keys

This commit is contained in:
Claudio Sanches 2017-06-22 18:06:10 -03:00
parent bddd45fb22
commit 1806590e71
1 changed files with 30 additions and 15 deletions

View File

@ -399,21 +399,18 @@ class WC_Product_CSV_Importer_Controller {
__( 'Button text', 'woocommerce' ) => 'button_text',
) );
$special_columns = array_map(
array( $this, 'sanitize_special_column_name_regex' ),
apply_filters( 'woocommerce_csv_product_import_mapping_special_columns',
array(
'attributes:name' => __( 'Attribute %d name', 'woocommerce' ),
'attributes:value' => __( 'Attribute %d value(s)', 'woocommerce' ),
'attributes:visible' => __( 'Attribute %d visible', 'woocommerce' ),
'attributes:taxonomy' => __( 'Attribute %d global', 'woocommerce' ),
'attributes:default' => __( 'Attribute %d default', 'woocommerce' ),
'downloads:name' => __( 'Download %d name', 'woocommerce' ),
'downloads:url' => __( 'Download %d URL', 'woocommerce' ),
'meta:' => __( 'Meta: %s', 'woocommerce' ),
)
$special_columns = $this->get_special_columns( apply_filters( 'woocommerce_csv_product_import_mapping_special_columns',
array(
__( 'Attribute %d name', 'woocommerce' ) => 'attributes:name',
__( 'Attribute %d value(s)', 'woocommerce' ) => 'attributes:value',
__( 'Attribute %d visible', 'woocommerce' ) => 'attributes:visible',
__( 'Attribute %d global', 'woocommerce' ) => 'attributes:taxonomy',
__( 'Attribute %d default', 'woocommerce' ) => 'attributes:default',
__( 'Download %d name', 'woocommerce' ) => 'downloads:name',
__( 'Download %d URL', 'woocommerce' ) => 'downloads:url',
__( 'Meta: %s', 'woocommerce' ) => 'meta:',
)
);
) );
$headers = array();
foreach ( $raw_headers as $key => $field ) {
@ -423,7 +420,7 @@ class WC_Product_CSV_Importer_Controller {
if ( isset( $default_columns[ $field ] ) ) {
$headers[ $index ] = $default_columns[ $field ];
} else {
foreach ( $special_columns as $special_key => $regex ) {
foreach ( $special_columns as $regex => $special_key ) {
if ( preg_match( $regex, $field, $matches ) ) {
$headers[ $index ] = $special_key . $matches[1];
break;
@ -445,6 +442,24 @@ class WC_Product_CSV_Importer_Controller {
return '/' . str_replace( array( '%d', '%s' ), '(.*)', quotemeta( $value ) ) . '/';
}
/**
* Get special columns.
*
* @param array $columns Raw special columns.
* @return array
*/
protected function get_special_columns( $columns ) {
$formatted = array();
foreach ( $columns as $key => $value ) {
$regex = $this->sanitize_special_column_name_regex( $key );
$formatted[ $regex ] = $value;
}
return $formatted;
}
/**
* Get mapping options.
*