WC_Product_CSV_Importer_Controller
in package
Product importer controller - handles file upload and forms in admin.
Tags
Table of Contents
- CLEANUP_BATCH_SIZE = 100
- Number of temporary products deleted per query when cleaning up after an import.
- CLEANUP_CLAIMED_STATUS = 'importing-cleanup'
- Status a temporary product is moved to once cleanup has claimed it for deletion.
- CLEANUP_PLACEHOLDER_STATUS = 'importing'
- Status the importer gives the temporary products it creates for unresolved references.
- CLEANUP_POSITION_PREFIX = 'cleanup:'
- Prefix of the AJAX position that runs, and resumes, the cleanup phase.
- $character_encoding : string
- The character encoding to use to interpret the input file, or empty string for autodetect.
- $delimiter : string
- The current delimiter for the file being read.
- $errors : array<string|int, mixed>
- Errors.
- $file : string
- The path to the current file.
- $map_preferences : bool
- Whether to use previous mapping selections.
- $step : string
- The current import step.
- $steps : array<string|int, mixed>
- Progress steps.
- $update_existing : bool
- Whether to skip existing products.
- __construct() : mixed
- Constructor.
- auto_map_user_preferences() : array<string|int, mixed>
- Map columns using the user's latest import mappings.
- dispatch() : mixed
- Dispatch current step and show correct view.
- dispatch_ajax() : mixed
- Processes AJAX requests related to a product CSV import.
- get_importer() : WC_Product_CSV_Importer
- Get importer instance.
- get_next_step_link() : string
- Get the URL for the next step's screen.
- handle_upload() : string|WP_Error
- Handles the CSV upload and initial parsing of the file to prepare for displaying author import options.
- import() : mixed
- Import the file if it exists and is valid.
- is_file_valid_csv() : bool
- Check whether a file is a valid CSV file.
- upload_form_handler() : mixed
- Handle the upload form and store options.
- add_error() : mixed
- Add error message.
- auto_map_columns() : array<string|int, mixed>
- Auto map column names.
- done() : mixed
- Done step.
- get_mapping_options() : array<string|int, mixed>
- Get mapping options.
- get_special_columns() : array<string|int, mixed>
- Get special columns.
- get_valid_csv_filetypes() : array<string|int, mixed>
- Get all the valid filetypes for a CSV file.
- mapping_form() : mixed
- Mapping step.
- normalize_columns_names() : array<string|int, mixed>
- Columns to normalize.
- output_errors() : mixed
- Add error message.
- output_footer() : mixed
- Output footer view.
- output_header() : mixed
- Output header view.
- output_steps() : mixed
- Output steps view.
- sanitize_special_column_name_regex() : string
- Sanitize special column name regex.
- upload_form() : mixed
- Output information about the uploading process.
- validate_file_path() : void
- Runs before controller actions to check that the file used during the import is valid.
- cleanup_after_import() : int|null
- Remove temporary products and mapping data left by the importer.
- delete_claimed_placeholder() : void
- Claim one importer placeholder and delete it only if the claim succeeded.
- delete_original_id_markers() : void
- Remove temporary mappings between CSV IDs and imported products.
- dispatch_ajax_cleanup() : void
- Run one bounded pass of the post-import cleanup and tell the client where to resume.
- release_stranded_cleanup_claims() : void
- Release temporary products a cleanup request claimed but did not live to delete.
Constants
CLEANUP_BATCH_SIZE
Number of temporary products deleted per query when cleaning up after an import.
private
mixed
CLEANUP_BATCH_SIZE
= 100
CLEANUP_CLAIMED_STATUS
Status a temporary product is moved to once cleanup has claimed it for deletion.
private
mixed
CLEANUP_CLAIMED_STATUS
= 'importing-cleanup'
CLEANUP_PLACEHOLDER_STATUS
Status the importer gives the temporary products it creates for unresolved references.
private
mixed
CLEANUP_PLACEHOLDER_STATUS
= 'importing'
CLEANUP_POSITION_PREFIX
Prefix of the AJAX position that runs, and resumes, the cleanup phase.
private
mixed
CLEANUP_POSITION_PREFIX
= 'cleanup:'
Properties
$character_encoding
The character encoding to use to interpret the input file, or empty string for autodetect.
protected
string
$character_encoding
= 'UTF-8'
$delimiter
The current delimiter for the file being read.
protected
string
$delimiter
= ','
$errors
Errors.
protected
array<string|int, mixed>
$errors
= array()
$file
The path to the current file.
protected
string
$file
= ''
$map_preferences
Whether to use previous mapping selections.
protected
bool
$map_preferences
= alse
$step
The current import step.
protected
string
$step
= ''
$steps
Progress steps.
protected
array<string|int, mixed>
$steps
= array()
$update_existing
Whether to skip existing products.
protected
bool
$update_existing
= alse
Methods
__construct()
Constructor.
public
__construct() : mixed
Return values
mixed —auto_map_user_preferences()
Map columns using the user's latest import mappings.
public
auto_map_user_preferences(array<string|int, mixed> $headers) : array<string|int, mixed>
Parameters
- $headers : array<string|int, mixed>
-
Header columns.
Return values
array<string|int, mixed> —dispatch()
Dispatch current step and show correct view.
public
dispatch() : mixed
Return values
mixed —dispatch_ajax()
Processes AJAX requests related to a product CSV import.
public
static dispatch_ajax() : mixed
Tags
Return values
mixed —get_importer()
Get importer instance.
public
static get_importer(string $file[, array<string|int, mixed> $args = array() ]) : WC_Product_CSV_Importer
Parameters
- $file : string
-
File to import.
- $args : array<string|int, mixed> = array()
-
Importer arguments.
Return values
WC_Product_CSV_Importer —get_next_step_link()
Get the URL for the next step's screen.
public
get_next_step_link([string $step = '' ]) : string
Parameters
- $step : string = ''
-
slug (default: current step).
Return values
string — URL for next step if a next step exists. Admin URL if it's the last step. Empty string on failure.handle_upload()
Handles the CSV upload and initial parsing of the file to prepare for displaying author import options.
public
handle_upload() : string|WP_Error
Return values
string|WP_Error —import()
Import the file if it exists and is valid.
public
import() : mixed
Return values
mixed —is_file_valid_csv()
Check whether a file is a valid CSV file.
public
static is_file_valid_csv(string $file[, bool $check_path = true ]) : bool
Parameters
- $file : string
-
File path.
- $check_path : bool = true
-
Whether to also check the file is located in a valid location (Default: true).
Return values
bool —upload_form_handler()
Handle the upload form and store options.
public
upload_form_handler() : mixed
Return values
mixed —add_error()
Add error message.
protected
add_error(string $message[, array<string|int, mixed> $actions = array() ]) : mixed
Parameters
- $message : string
-
Error message.
- $actions : array<string|int, mixed> = array()
-
List of actions with 'url' and 'label'.
Return values
mixed —auto_map_columns()
Auto map column names.
protected
auto_map_columns(array<string|int, mixed> $raw_headers[, bool $num_indexes = true ]) : array<string|int, mixed>
Parameters
- $raw_headers : array<string|int, mixed>
-
Raw header columns.
- $num_indexes : bool = true
-
If should use numbers or raw header columns as indexes.
Return values
array<string|int, mixed> —done()
Done step.
protected
done() : mixed
Return values
mixed —get_mapping_options()
Get mapping options.
protected
get_mapping_options([string $item = '' ]) : array<string|int, mixed>
Parameters
- $item : string = ''
-
Item name.
Return values
array<string|int, mixed> —get_special_columns()
Get special columns.
protected
get_special_columns(array<string|int, mixed> $columns) : array<string|int, mixed>
Parameters
- $columns : array<string|int, mixed>
-
Raw special columns.
Return values
array<string|int, mixed> —get_valid_csv_filetypes()
Get all the valid filetypes for a CSV file.
protected
static get_valid_csv_filetypes() : array<string|int, mixed>
Return values
array<string|int, mixed> —mapping_form()
Mapping step.
protected
mapping_form() : mixed
Return values
mixed —normalize_columns_names()
Columns to normalize.
protected
normalize_columns_names(array<string|int, mixed> $columns) : array<string|int, mixed>
Parameters
- $columns : array<string|int, mixed>
-
List of columns names and keys.
Return values
array<string|int, mixed> —output_errors()
Add error message.
protected
output_errors() : mixed
Return values
mixed —output_footer()
Output footer view.
protected
output_footer() : mixed
Return values
mixed —output_header()
Output header view.
protected
output_header() : mixed
Return values
mixed —output_steps()
Output steps view.
protected
output_steps() : mixed
Return values
mixed —sanitize_special_column_name_regex()
Sanitize special column name regex.
protected
sanitize_special_column_name_regex(string $value) : string
Parameters
- $value : string
-
Raw special column name.
Return values
string —upload_form()
Output information about the uploading process.
protected
upload_form() : mixed
Return values
mixed —validate_file_path()
Runs before controller actions to check that the file used during the import is valid.
protected
static validate_file_path(string $path) : void
Parameters
- $path : string
-
Path to test.
Tags
Return values
void —cleanup_after_import()
Remove temporary products and mapping data left by the importer.
private
static cleanup_after_import(int $cursor) : int|null
Parameters
- $cursor : int
-
Highest placeholder ID earlier cleanup requests have examined.
Return values
int|null — ID to resume from, or null once no placeholders are left.delete_claimed_placeholder()
Claim one importer placeholder and delete it only if the claim succeeded.
private
static delete_claimed_placeholder(int $post_id) : void
Reading the status from the object cache cannot decide this: a process that completes a placeholder does not invalidate this process's cache, so a published product would still look like a placeholder and be force deleted. The guarded UPDATE makes the status check atomic instead, the way ActionScheduler_DBStore::claim_actions() claims actions, and runs per post so a placeholder completed while the batch is being deleted is still protected.
Parameters
- $post_id : int
-
Candidate post ID.
Return values
void —delete_original_id_markers()
Remove temporary mappings between CSV IDs and imported products.
private
static delete_original_id_markers() : void
The last import batch clears them so a run abandoned during cleanup leaves none behind. Markers from a run abandoned earlier are kept so a retry of the same file skips rows already imported. Concurrent imports are unsupported: markers and placeholder cleanup are shared site-wide.
Return values
void —dispatch_ajax_cleanup()
Run one bounded pass of the post-import cleanup and tell the client where to resume.
private
static dispatch_ajax_cleanup(int $cursor) : void
The cursor only ever skips placeholders forward, and the queries it feeds match nothing but the importer's own temporary products, so it needs no server-side state of its own.
Parameters
- $cursor : int
-
Highest placeholder ID earlier cleanup requests have examined.
Return values
void —release_stranded_cleanup_claims()
Release temporary products a cleanup request claimed but did not live to delete.
private
static release_stranded_cleanup_claims() : void
A fatal inside the delete lifecycle leaves the claimed status behind, and the importer only recognises a placeholder by the status it was created with, so the CSV row owning that SKU would be skipped as an existing product. Healing at the start of a run keeps that contained.
