key ?? '', self::KEYS, true ) && is_array( $value ) ) { if ( Tabs::META_LAYOUT === $meta->key ) { $value = Transfer::export_layout( $value ); // Shared tabs with a reference that holds on another site. } return (string) wp_json_encode( $value, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE ); } return $value; } /** * Marks of the YIKES import stay out of the export: they describe this site only. * * @param array $keys Meta keys left out. */ public static function skip_keys( $keys ) { return array_merge( (array) $keys, array( Import_Yikes::META_IMPORTED, Import_Yikes::META_BACKUP ) ); } /** * Takes the tab columns out of the meta that WooCommerce writes, to write them once the product is saved. * * @param array $data Parsed row. */ public static function parsed_data( $data ) { if ( empty( $data['meta_data'] ) || ! is_array( $data['meta_data'] ) ) { return $data; } foreach ( $data['meta_data'] as $index => $meta ) { if ( ! isset( $meta['key'] ) || ! in_array( $meta['key'], self::KEYS, true ) ) { continue; } unset( $data['meta_data'][ $index ] ); // The CSV value comes raw (no slashes added): the escapes of the JSON itself must stay. $value = is_string( $meta['value'] ?? null ) ? json_decode( $meta['value'], true ) : null; // An empty cell or a value that is not ours leaves the tabs of the product as they are. if ( is_array( $value ) ) { $data[ self::DATA ][ $meta['key'] ] = $value; } } $data['meta_data'] = array_values( $data['meta_data'] ); return $data; } /** * Writes the imported tabs, pointed to the shared tabs of this site. update_post_meta() removes one level of * slashes: the value is slashed first. * * @param \WC_Product $product Imported product. * @param array $data Parsed row. */ public static function write( $product, $data ): void { if ( empty( $data[ self::DATA ] ) || ! $product instanceof \WC_Product ) { return; } $id = $product->get_id(); foreach ( $data[ self::DATA ] as $key => $value ) { if ( Tabs::META_LAYOUT === $key && is_array( $value ) ) { $value = Transfer::import_layout( $value ); // Shared tabs of this site, found by their reference. } if ( $value ) { update_post_meta( $id, $key, wp_slash( $value ) ); } else { delete_post_meta( $id, $key ); } } clean_post_cache( $id ); wc_delete_product_transients( $id ); } }