Merge pull request #18082 from WPprodigy/remember-importer-mappings

Remember importer mappings
This commit is contained in:
Claudio Sanches 2018-02-23 11:05:24 -03:00 committed by GitHub
commit 9db8a434b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 1 deletions

View File

@ -52,6 +52,13 @@ class WC_Product_CSV_Importer_Controller {
*/ */
protected $delimiter = ','; protected $delimiter = ',';
/**
* Whether to use previous mapping selections.
*
* @var bool
*/
protected $map_preferences = false;
/** /**
* Whether to skip existing products. * Whether to skip existing products.
* *
@ -105,6 +112,7 @@ class WC_Product_CSV_Importer_Controller {
$this->file = isset( $_REQUEST['file'] ) ? wc_clean( $_REQUEST['file'] ) : ''; $this->file = isset( $_REQUEST['file'] ) ? wc_clean( $_REQUEST['file'] ) : '';
$this->update_existing = isset( $_REQUEST['update_existing'] ) ? (bool) $_REQUEST['update_existing'] : false; $this->update_existing = isset( $_REQUEST['update_existing'] ) ? (bool) $_REQUEST['update_existing'] : false;
$this->delimiter = ! empty( $_REQUEST['delimiter'] ) ? wc_clean( $_REQUEST['delimiter'] ) : ','; $this->delimiter = ! empty( $_REQUEST['delimiter'] ) ? wc_clean( $_REQUEST['delimiter'] ) : ',';
$this->map_preferences = isset( $_REQUEST['map_preferences'] ) ? (bool) $_REQUEST['map_preferences'] : false;
} }
/** /**
@ -136,6 +144,7 @@ class WC_Product_CSV_Importer_Controller {
'file' => str_replace( DIRECTORY_SEPARATOR, '/', $this->file ), 'file' => str_replace( DIRECTORY_SEPARATOR, '/', $this->file ),
'delimiter' => $this->delimiter, 'delimiter' => $this->delimiter,
'update_existing' => $this->update_existing, 'update_existing' => $this->update_existing,
'map_preferences' => $this->map_preferences,
'_wpnonce' => wp_create_nonce( 'woocommerce-csv-importer' ), // wp_nonce_url() escapes & to & breaking redirects. '_wpnonce' => wp_create_nonce( 'woocommerce-csv-importer' ), // wp_nonce_url() escapes & to & breaking redirects.
); );
@ -310,6 +319,10 @@ class WC_Product_CSV_Importer_Controller {
'delimiter' => $this->delimiter, 'delimiter' => $this->delimiter,
); );
if ( $this->map_preferences ) {
add_filter( 'woocommerce_csv_product_import_mapped_columns', array( $this, 'auto_map_user_preferences' ), 99 );
}
$importer = self::get_importer( $this->file, $args ); $importer = self::get_importer( $this->file, $args );
$headers = $importer->get_raw_keys(); $headers = $importer->get_raw_keys();
$mapped_items = $this->auto_map_columns( $headers ); $mapped_items = $this->auto_map_columns( $headers );
@ -346,6 +359,9 @@ class WC_Product_CSV_Importer_Controller {
if ( ! empty( $_POST['map_to'] ) ) { if ( ! empty( $_POST['map_to'] ) ) {
$mapping_from = wp_unslash( $_POST['map_from'] ); $mapping_from = wp_unslash( $_POST['map_from'] );
$mapping_to = wp_unslash( $_POST['map_to'] ); $mapping_to = wp_unslash( $_POST['map_to'] );
// Save mapping preferences for future imports.
update_user_option( get_current_user_id(), 'product_import_mapping_preferences', $mapping_to );
} else { } else {
wp_redirect( esc_url_raw( $this->get_next_step_link( 'upload' ) ) ); wp_redirect( esc_url_raw( $this->get_next_step_link( 'upload' ) ) );
exit; exit;
@ -488,6 +504,22 @@ class WC_Product_CSV_Importer_Controller {
return apply_filters( 'woocommerce_csv_product_import_mapped_columns', $headers, $raw_headers ); return apply_filters( 'woocommerce_csv_product_import_mapped_columns', $headers, $raw_headers );
} }
/**
* Map columns using the user's lastest import mappings.
*
* @param array $headers Header columns.
* @return array
*/
public function auto_map_user_preferences( $headers ) {
$mapping_preferences = get_user_option( 'product_import_mapping_preferences' );
if ( ! empty( $mapping_preferences ) ) {
return $mapping_preferences;
}
return $headers;
}
/** /**
* Sanitize special column name regex. * Sanitize special column name regex.
* *

View File

@ -60,7 +60,7 @@ if ( ! defined( 'ABSPATH' ) ) {
</tr> </tr>
<tr class="woocommerce-importer-advanced hidden"> <tr class="woocommerce-importer-advanced hidden">
<th> <th>
<label for="woocommerce-importer-file-url"><?php esc_html_e( '<em>or</em> enter the path to a CSV file on your server:', 'woocommerce' ); ?></label> <label for="woocommerce-importer-file-url"><?php esc_html_e( 'Alternatively, enter the path to a CSV file on your server:', 'woocommerce' ); ?></label>
</th> </th>
<td> <td>
<label for="woocommerce-importer-file-url" class="woocommerce-importer-file-url-field-wrapper"> <label for="woocommerce-importer-file-url" class="woocommerce-importer-file-url-field-wrapper">
@ -72,6 +72,10 @@ if ( ! defined( 'ABSPATH' ) ) {
<th><label><?php esc_html_e( 'CSV Delimiter', 'woocommerce' ); ?></label><br/></th> <th><label><?php esc_html_e( 'CSV Delimiter', 'woocommerce' ); ?></label><br/></th>
<td><input type="text" name="delimiter" placeholder="," size="2" /></td> <td><input type="text" name="delimiter" placeholder="," size="2" /></td>
</tr> </tr>
<tr class="woocommerce-importer-advanced hidden">
<th><label><?php esc_html_e( 'Use previous column mapping preferences?', 'woocommerce' ); ?></label><br/></th>
<td><input type="checkbox" id="woocommerce-importer-map-preferences" name="map_preferences" value="1" /></td>
</tr>
</tbody> </tbody>
</table> </table>
</section> </section>