field; * N stands for the attribute number. */ const WC_COLUMNS = array( 'ID' => 'id', 'Type' => 'type', 'SKU' => 'sku', 'GTIN, UPC, EAN, or ISBN' => 'global_unique_id', 'Name' => 'name', 'Published' => 'status', 'Is featured?' => 'featured', 'Visibility in catalog' => 'catalog_visibility', 'Short description' => 'short_description', 'Description' => 'description', 'Tax status' => 'tax_status', 'Tax class' => 'tax_class', 'In stock?' => 'stock_status', 'Stock' => 'stock_quantity', 'Low stock amount' => 'low_stock_amount', 'Backorders allowed?' => 'backorders', 'Sold individually?' => 'sold_individually', 'Weight (%s)' => 'weight', 'Length (%s)' => 'length', 'Width (%s)' => 'width', 'Height (%s)' => 'height', 'Allow customer reviews?' => 'reviews_allowed', 'Purchase note' => 'purchase_note', 'Sale price' => 'sale_price', 'Regular price' => 'regular_price', 'Categories' => 'categories', 'Tags' => 'tags', 'Shipping class' => 'shipping_class', 'Images' => 'images', 'Parent' => 'parent', 'External URL' => 'product_url', 'Button text' => 'button_text', 'Position' => 'menu_order', 'Attribute %d name' => 'attr:N:name', 'Attribute %d value(s)' => 'attr:N:values', 'Attribute %d visible' => 'attr:N:visible', 'Attribute %d global' => 'attr:N:global', 'Attribute %d default' => 'attr:N:default', ); /** * Definitions: key => [group, kind, ledger, aliases]. * * @return array */ public static function definitions(): array { return array( 'id' => array( 'match', 'int', '', array( 'id', 'product id', 'post id' ) ), 'type' => array( 'structure', 'type', '', array( 'type', 'product type', 'tax:product_type' ) ), 'sku' => array( 'match', 'text', '', array( 'sku', 'product sku' ) ), 'global_unique_id' => array( 'match', 'text', '', array( 'gtin, upc, ean, or isbn', 'gtin', 'upc', 'ean', 'isbn', 'global unique id', 'barcode' ) ), 'parent' => array( 'structure', 'text', '', array( 'parent', 'parent sku', 'post parent', 'parent id' ) ), 'name' => array( 'content', 'text', '', array( 'name', 'title', 'post title', 'product name' ) ), 'status' => array( 'status', 'status', 'status', array( 'published', 'post status', 'status' ) ), 'featured' => array( 'content', 'bool', '', array( 'is featured', 'featured' ) ), 'catalog_visibility' => array( 'content', 'text', '', array( 'visibility in catalog', 'visibility', 'catalog visibility' ) ), 'short_description' => array( 'content', 'text', '', array( 'short description', 'post excerpt', 'excerpt' ) ), 'description' => array( 'content', 'text', '', array( 'description', 'post content', 'content' ) ), 'tax_status' => array( 'tax', 'text', '', array( 'tax status' ) ), 'tax_class' => array( 'tax', 'text', '', array( 'tax class' ) ), 'stock_status' => array( 'stock', 'stock_status', 'stock', array( 'in stock', 'stock status' ) ), 'stock_quantity' => array( 'stock', 'stock', 'stock', array( 'stock', 'stock quantity', 'quantity' ) ), 'manage_stock' => array( 'stock', 'bool', 'stock', array( 'manage stock' ) ), 'low_stock_amount' => array( 'stock', 'int', '', array( 'low stock amount' ) ), 'backorders' => array( 'stock', 'text', 'stock', array( 'backorders allowed', 'backorders' ) ), 'sold_individually' => array( 'stock', 'bool', '', array( 'sold individually' ) ), 'weight' => array( 'shipping', 'decimal', '', array( 'weight' ) ), 'length' => array( 'shipping', 'decimal', '', array( 'length' ) ), 'width' => array( 'shipping', 'decimal', '', array( 'width' ) ), 'height' => array( 'shipping', 'decimal', '', array( 'height' ) ), 'reviews_allowed' => array( 'content', 'bool', '', array( 'allow customer reviews', 'reviews allowed' ) ), 'purchase_note' => array( 'content', 'text', '', array( 'purchase note' ) ), 'sale_price' => array( 'price', 'price', 'price', array( 'sale price' ) ), 'regular_price' => array( 'price', 'price', 'price', array( 'regular price', 'price' ) ), 'categories' => array( 'taxonomy', 'list', '', array( 'categories', 'category', 'tax:product_cat' ) ), 'tags' => array( 'taxonomy', 'list', '', array( 'tags', 'tax:product_tag' ) ), 'shipping_class' => array( 'shipping', 'text', '', array( 'shipping class', 'tax:product_shipping_class' ) ), 'images' => array( 'media', 'list', '', array( 'images', 'image', 'image url' ) ), 'product_url' => array( 'structure', 'text', '', array( 'external url', 'product url' ) ), 'button_text' => array( 'structure', 'text', '', array( 'button text' ) ), 'menu_order' => array( 'content', 'int', '', array( 'position', 'menu order' ) ), ); } /** * Alias lookup (normalized alias => field key), built once. * * @return array */ private static function aliasMap(): array { static $map = null; if ( null === $map ) { $map = array(); foreach ( self::definitions() as $key => $def ) { $map[ HeaderNormalizer::key( $key ) ] = $key; foreach ( $def[3] as $alias ) { $k = HeaderNormalizer::key( $alias ); if ( ! isset( $map[ $k ] ) ) { $map[ $k ] = $key; } } } } return $map; } /** * Field key a header belongs to: a definition key, "attr:N:name|values|visible|global|default", or null. * * @param string $header Header text. * @return string|null */ public static function resolve( string $header ): ?string { $k = HeaderNormalizer::key( $header ); if ( '' === $k ) { return null; } if ( 1 === preg_match( '/^attribute (\d+) (name|values|value|visible|global|default)$/', $k, $m ) ) { return self::ATTR_PREFIX . (int) $m[1] . ':' . ( 'value' === $m[2] ? 'values' : $m[2] ); } $map = self::aliasMap(); if ( function_exists( 'apply_filters' ) ) { /** * Filters extra header aliases (alias text => field key), e.g. a supplier's own column names. * * @param array $extra Aliases. */ foreach ( (array) apply_filters( 'catalogrelay_mapping_aliases', array() ) as $alias => $field ) { if ( is_string( $field ) && self::isField( $field ) && HeaderNormalizer::key( (string) $alias ) === $k ) { return $field; } } } if ( isset( $map[ $k ] ) ) { return $map[ $k ]; } // "Weight (kg)", "Length (cm)" and the like: WooCommerce's exporter writes the store's unit. A known unit other than // this store's is left unmapped (as WooCommerce's importer does): the numbers would be wrong by the unit factor. if ( 1 === preg_match( '/^(weight|length|width|height) ([a-z]+)$/', $k, $m ) ) { return self::storeUnit( $m[1], $m[2] ) ? $m[1] : null; } return self::translated( $k ); } /** * Whether a unit named in a header is the store's unit (true when the unit is not one WooCommerce offers). * * @param string $dimension weight|length|width|height. * @param string $unit Unit from the header, lower case. * @return bool */ private static function storeUnit( string $dimension, string $unit ): bool { $weight = 'weight' === $dimension; $known = $weight ? array( 'kg', 'g', 'lbs', 'lb', 'oz' ) : array( 'm', 'cm', 'mm', 'in', 'yd' ); if ( ! in_array( $unit, $known, true ) || ! function_exists( 'get_option' ) ) { return true; } $store = strtolower( (string) ( $weight ? get_option( 'woocommerce_weight_unit', 'kg' ) : get_option( 'woocommerce_dimension_unit', 'cm' ) ) ); return ( 'lb' === $unit ? 'lbs' : $unit ) === $store; } /** * Column names of WooCommerce's exporter in the current language (a store whose admin is not in English exports * translated headers; WooCommerce's importer maps them the same way). * * @param string $k Normalized header. * @return string|null */ private static function translated( string $k ): ?string { if ( ! function_exists( 'translate' ) ) { return null; } static $cache = array(); $locale = function_exists( 'determine_locale' ) ? determine_locale() : ''; if ( ! isset( $cache[ $locale ] ) ) { $exact = array(); $patterns = array(); foreach ( self::WC_COLUMNS as $label => $field ) { $text = translate( $label, 'woocommerce' ); // phpcs:ignore WordPress.WP.I18n -- WooCommerce's own strings, looked up to read its export. if ( $text === $label ) { continue; // Not translated: the English aliases cover it. } $parts = preg_split( '/%(?:\d+\$)?[sd]/', $text ); if ( 1 === count( $parts ) ) { $exact += array( HeaderNormalizer::key( $text ) => $field ); continue; } $regex = array(); foreach ( $parts as $i => $part ) { if ( $i > 0 ) { $regex[] = false !== strpos( $label, '%d' ) ? '(\d+)' : '(.+)'; } $key = HeaderNormalizer::key( $part ); if ( '' !== $key ) { $regex[] = preg_quote( $key, '/' ); } } $patterns[ '/^' . implode( ' ', $regex ) . '$/u' ] = $field; } unset( $exact[''] ); $cache[ $locale ] = array( $exact, $patterns ); } list( $exact, $patterns ) = $cache[ $locale ]; if ( isset( $exact[ $k ] ) ) { return $exact[ $k ]; } foreach ( $patterns as $regex => $field ) { if ( 1 === preg_match( $regex, $k, $m ) ) { if ( 0 === strpos( $field, self::ATTR_PREFIX ) ) { return str_replace( 'N', (string) (int) $m[1], $field ); } return self::storeUnit( $field, $m[1] ) ? $field : null; // Weight and dimensions with their unit. } } return null; } /** * Whether a header is a field's canonical name (its key, its first alias, or any name other than its further aliases): * such a column takes the field from a column matched by a further, more generic alias ("Regular price" over "Price"). * * @param string $header Header text. * @param string $field Field key. * @return bool */ public static function isCanonical( string $header, string $field ): bool { $d = self::definitions(); if ( ! isset( $d[ $field ] ) ) { return true; } $k = HeaderNormalizer::key( $header ); $further = array_map( array( HeaderNormalizer::class, 'key' ), array_slice( $d[ $field ][3], 1 ) ); $own = array( HeaderNormalizer::key( $field ), HeaderNormalizer::key( (string) $d[ $field ][3][0] ) ); return in_array( $k, $own, true ) || ! in_array( $k, $further, true ); } /** * Whether a key is a known field. * * @param string $key Key. * @return bool */ public static function isField( string $key ): bool { return isset( self::definitions()[ $key ] ) || 1 === preg_match( '/^attr:\d+:(name|values|visible|global|default)$/', $key ); } /** * Parse kind of a field. * * @param string $key Field key. * @return string */ public static function kind( string $key ): string { $d = self::definitions(); return isset( $d[ $key ] ) ? $d[ $key ][1] : 'text'; } /** * Group of a field. * * @param string $key Field key. * @return string */ public static function group( string $key ): string { $d = self::definitions(); if ( isset( $d[ $key ] ) ) { return $d[ $key ][0]; } return 0 === strpos( $key, self::ATTR_PREFIX ) ? 'attributes' : 'other'; } /** * Snapshot adapter group of a field ('price', 'stock', 'status' or ''). * * @param string $key Field key. * @return string */ public static function ledgerGroup( string $key ): string { $d = self::definitions(); return isset( $d[ $key ] ) ? $d[ $key ][2] : ''; } /** * Human label of a field. * * @param string $key Field key. * @return string */ public static function label( string $key ): string { $labels = array( 'id' => __( 'ID', 'catalogrelay' ), 'type' => __( 'Type', 'catalogrelay' ), 'sku' => __( 'SKU', 'catalogrelay' ), 'global_unique_id' => __( 'GTIN, UPC, EAN or ISBN', 'catalogrelay' ), 'parent' => __( 'Parent', 'catalogrelay' ), 'name' => __( 'Name', 'catalogrelay' ), 'status' => __( 'Published', 'catalogrelay' ), 'featured' => __( 'Is featured?', 'catalogrelay' ), 'catalog_visibility' => __( 'Visibility in catalog', 'catalogrelay' ), 'short_description' => __( 'Short description', 'catalogrelay' ), 'description' => __( 'Description', 'catalogrelay' ), 'tax_status' => __( 'Tax status', 'catalogrelay' ), 'tax_class' => __( 'Tax class', 'catalogrelay' ), 'stock_status' => __( 'In stock?', 'catalogrelay' ), 'stock_quantity' => __( 'Stock', 'catalogrelay' ), 'manage_stock' => __( 'Manage stock', 'catalogrelay' ), 'low_stock_amount' => __( 'Low stock amount', 'catalogrelay' ), 'backorders' => __( 'Backorders allowed?', 'catalogrelay' ), 'sold_individually' => __( 'Sold individually?', 'catalogrelay' ), 'weight' => __( 'Weight', 'catalogrelay' ), 'length' => __( 'Length', 'catalogrelay' ), 'width' => __( 'Width', 'catalogrelay' ), 'height' => __( 'Height', 'catalogrelay' ), 'reviews_allowed' => __( 'Allow customer reviews?', 'catalogrelay' ), 'purchase_note' => __( 'Purchase note', 'catalogrelay' ), 'sale_price' => __( 'Sale price', 'catalogrelay' ), 'regular_price' => __( 'Regular price', 'catalogrelay' ), 'categories' => __( 'Categories', 'catalogrelay' ), 'tags' => __( 'Tags', 'catalogrelay' ), 'shipping_class' => __( 'Shipping class', 'catalogrelay' ), 'images' => __( 'Images', 'catalogrelay' ), 'product_url' => __( 'External URL', 'catalogrelay' ), 'button_text' => __( 'Button text', 'catalogrelay' ), 'menu_order' => __( 'Position', 'catalogrelay' ), ); if ( isset( $labels[ $key ] ) ) { return $labels[ $key ]; } if ( 1 === preg_match( '/^attr:(\d+):(\w+)$/', $key, $m ) ) { /* translators: 1: attribute number, 2: part (name, values, visible, global, default) */ return sprintf( __( 'Attribute %1$d %2$s', 'catalogrelay' ), (int) $m[1], $m[2] ); } return $key; } /** * All field keys with labels and groups, for the mapping UI. * * @return array[] */ public static function catalog(): array { $out = array(); foreach ( array_keys( self::definitions() ) as $key ) { $out[] = array( 'key' => $key, 'label' => self::label( $key ), 'group' => self::group( $key ), 'ledger' => self::ledgerGroup( $key ), ); } return $out; } }