CSV_Importer
This class is the controller for the CSV Import
Tags
Table of Contents
- $file : string
- CSV file.
- $file_position : int
- The file position after the last read.
- $file_positions : array<string|int, mixed>
- Raw data.
- $mapped_keys : array<string|int, mixed>
- Mapped keys - CSV headers.
- $params : array<string|int, mixed>
- Importer parameters.
- $parsed_data : array<string|int, mixed>
- Parsed data.
- $parsing_raw_data_index : int
- Tracks current row being parsed.
- $raw_data : array<string|int, mixed>
- Raw data.
- $raw_keys : array<string|int, mixed>
- Raw keys - CSV raw headers.
- $start_time : int
- Start time of current import.
- $taxonomies : array<string|int, mixed>
- A list of taxonomies registered to the Document post type.
- __construct() : mixed
- Initialize importer.
- get_file_position() : int
- Get file pointer position from the last read.
- get_mapped_keys() : array<string|int, mixed>
- Get file mapped headers.
- get_params() : array<string|int, mixed>
- Get importer parameters.
- get_parsed_data() : array<string|int, mixed>
- Get parsed data.
- get_percent_complete() : int
- Get file pointer position as a percentage of file size.
- get_raw_data() : array<string|int, mixed>
- Get raw data.
- get_raw_keys() : array<string|int, mixed>
- Get file raw headers.
- get_taxonomy_by_name_or_label() : WP_Taxonomy
- Get a taxonomy object by its label (plural)
- import() : array<string|int, mixed>
- Process importer.
- parse_author_field() : array<string|int, mixed>
- Parse a tag field from a CSV.
- parse_bool_field() : bool|string
- Parse a field that is generally '1' or '0' but can be something else.
- parse_categories_field() : array<string|int, mixed>
- Parse a category field from a CSV.
- parse_date_field() : string|null
- Parse dates from a CSV.
- parse_description_field() : string
- Parse a description value field
- parse_expiry_field() : string|null
- Parse dates from a CSV.
- parse_float_field() : float|string
- Parse a float value field.
- parse_hierarchical_taxonomy_field() : array<string|int, mixed>
- Parse a hierarchical taxonomy field from a CSV.
- parse_int_field() : int
- Parse an int value field
- parse_non_hierarchical_taxonomy_field() : array<string|int, mixed>
- Parse a non-hierarchical taxonomy field from a CSV.
- parse_published_field() : float|string
- Parse the published field. 1 is published, 0 is private, -1 is draft.
- parse_skip_field() : string
- Just skip current field.
- parse_tags_field() : array<string|int, mixed>
- Parse a tag field from a CSV.
- parse_tags_spaces_field() : array<string|int, mixed>
- Parse a tag field from a CSV with space separators.
- parse_taxonomies_field() : array<string|int, mixed>
- Parse a taxonomy field and return a list of term ids.
- parse_url_field() : string|false
- Parse the URL field.
- expand_data() : array<string|int, mixed>
- Expand special and internal data into the correct formats for the product CRUD.
- explode_values() : array<string|int, mixed>
- Explode CSV cell values using commas by default, and handling escaped separators.
- explode_values_formatter() : string
- Remove formatting and trim each value.
- get_formatting_callback() : array<string|int, mixed>
- Get formatting callback.
- get_memory_limit() : int
- Get memory limit
- get_row_id() : string
- Get a string to identify the row from parsed data.
- memory_exceeded() : bool
- Memory exceeded
- process_item() : array<string|int, mixed>|WP_Error
- Process a single item and save.
- read_file() : mixed
- Read file.
- remove_utf8_bom() : string
- Remove UTF-8 BOM signature.
- set_mapped_keys() : mixed
- Set file mapped keys.
- set_parsed_data() : mixed
- Map and format raw data to known fields.
- starts_with() : bool
- Check if strings starts with determined word.
- time_exceeded() : bool
- Time exceeded.
- unescape_data() : string
- The exporter prepends a ' to escape fields that start with =, +, - or @.
Properties
$file
CSV file.
protected
string
$file
= ''
$file_position
The file position after the last read.
protected
int
$file_position
= 0
$file_positions
Raw data.
protected
array<string|int, mixed>
$file_positions
= []
$mapped_keys
Mapped keys - CSV headers.
protected
array<string|int, mixed>
$mapped_keys
= []
$params
Importer parameters.
protected
array<string|int, mixed>
$params
= []
$parsed_data
Parsed data.
protected
array<string|int, mixed>
$parsed_data
= []
$parsing_raw_data_index
Tracks current row being parsed.
protected
int
$parsing_raw_data_index
= 0
$raw_data
Raw data.
protected
array<string|int, mixed>
$raw_data
= []
$raw_keys
Raw keys - CSV raw headers.
protected
array<string|int, mixed>
$raw_keys
= []
$start_time
Start time of current import.
protected
int
$start_time
= 0
(default value: 0)
$taxonomies
A list of taxonomies registered to the Document post type.
protected
array<string|int, mixed>
$taxonomies
= []
Methods
__construct()
Initialize importer.
public
__construct(string $file[, array<string|int, mixed> $params = [] ]) : mixed
Parameters
- $file : string
-
File to read.
- $params : array<string|int, mixed> = []
-
Arguments for the parser.
Return values
mixed —get_file_position()
Get file pointer position from the last read.
public
get_file_position() : int
Return values
int —get_mapped_keys()
Get file mapped headers.
public
get_mapped_keys() : array<string|int, mixed>
Return values
array<string|int, mixed> —get_params()
Get importer parameters.
public
get_params() : array<string|int, mixed>
Return values
array<string|int, mixed> —get_parsed_data()
Get parsed data.
public
get_parsed_data() : array<string|int, mixed>
Return values
array<string|int, mixed> —get_percent_complete()
Get file pointer position as a percentage of file size.
public
get_percent_complete() : int
Return values
int —get_raw_data()
Get raw data.
public
get_raw_data() : array<string|int, mixed>
Return values
array<string|int, mixed> —get_raw_keys()
Get file raw headers.
public
get_raw_keys() : array<string|int, mixed>
Return values
array<string|int, mixed> —get_taxonomy_by_name_or_label()
Get a taxonomy object by its label (plural)
public
get_taxonomy_by_name_or_label(string $name_label) : WP_Taxonomy
Parameters
- $name_label : string
-
The name or the label of the taxonomy.
Return values
WP_Taxonomy —import()
Process importer.
public
import() : array<string|int, mixed>
Return values
array<string|int, mixed> —parse_author_field()
Parse a tag field from a CSV.
public
parse_author_field(string $value) : array<string|int, mixed>
Parameters
- $value : string
-
Field value.
Return values
array<string|int, mixed> —parse_bool_field()
Parse a field that is generally '1' or '0' but can be something else.
public
parse_bool_field(string $value) : bool|string
Parameters
- $value : string
-
Field value.
Return values
bool|string —parse_categories_field()
Parse a category field from a CSV.
public
parse_categories_field(string $value) : array<string|int, mixed>
Categories are separated by commas and subcategories are "parent > subcategory".
Parameters
- $value : string
-
Field value.
Return values
array<string|int, mixed> —of arrays with "parent" and "name" keys.
parse_date_field()
Parse dates from a CSV.
public
parse_date_field(string $value) : string|null
Dates requires the format YYYY-MM-DD and time is optional.
Parameters
- $value : string
-
Field value.
Return values
string|null —parse_description_field()
Parse a description value field
public
parse_description_field(string $description) : string
Parameters
- $description : string
-
field value.
Return values
string —parse_expiry_field()
Parse dates from a CSV.
public
parse_expiry_field(string $value) : string|null
Allowed format is YYYY-MM-DD HH:MM (with the time optional).
Parameters
- $value : string
-
Field value.
Return values
string|null —parse_float_field()
Parse a float value field.
public
parse_float_field(string $value) : float|string
Parameters
- $value : string
-
Field value.
Return values
float|string —parse_hierarchical_taxonomy_field()
Parse a hierarchical taxonomy field from a CSV.
public
parse_hierarchical_taxonomy_field(string $taxonomy, string $value) : array<string|int, mixed>
Parameters
- $taxonomy : string
-
A taxonomy slug.
- $value : string
-
Field value.
Return values
array<string|int, mixed> —parse_int_field()
Parse an int value field
public
parse_int_field(int $value) : int
Parameters
- $value : int
-
field value.
Return values
int —parse_non_hierarchical_taxonomy_field()
Parse a non-hierarchical taxonomy field from a CSV.
public
parse_non_hierarchical_taxonomy_field(string $taxonomy, string $value[, string|null $separator = null ]) : array<string|int, mixed>
Parameters
- $taxonomy : string
-
A taxonomy slug.
- $value : string
-
Field value.
- $separator : string|null = null
-
A separator delimiting different terms.
Return values
array<string|int, mixed> —parse_published_field()
Parse the published field. 1 is published, 0 is private, -1 is draft.
public
parse_published_field(string $value) : float|string
Alternatively, 'true' can be used for published and 'false' for draft.
Parameters
- $value : string
-
Field value.
Return values
float|string —parse_skip_field()
Just skip current field.
public
parse_skip_field(string $value) : string
By default is applied sanitize_text_field() to all not listed fields in self::get_formatting_callback(), use this method to skip any formatting.
Parameters
- $value : string
-
Field value.
Return values
string —parse_tags_field()
Parse a tag field from a CSV.
public
parse_tags_field(string $value) : array<string|int, mixed>
Parameters
- $value : string
-
Field value.
Return values
array<string|int, mixed> —parse_tags_spaces_field()
Parse a tag field from a CSV with space separators.
public
parse_tags_spaces_field(string $value) : array<string|int, mixed>
Parameters
- $value : string
-
Field value.
Return values
array<string|int, mixed> —parse_taxonomies_field()
Parse a taxonomy field and return a list of term ids.
public
parse_taxonomies_field(string $name_label, string $value) : array<string|int, mixed>
This method returns an associative array with the taxonomy slug as a key so that the Document class can easily add it to the tax_input argument.
Parameters
- $name_label : string
-
The name or the label of the taxonomy.
- $value : string
-
Field value.
Return values
array<string|int, mixed> —parse_url_field()
Parse the URL field.
public
parse_url_field(mixed $value) : string|false
Parameters
- $value : mixed
Return values
string|false —expand_data()
Expand special and internal data into the correct formats for the product CRUD.
protected
expand_data(array<string|int, mixed> $data) : array<string|int, mixed>
Parameters
- $data : array<string|int, mixed>
-
Data to import.
Return values
array<string|int, mixed> —explode_values()
Explode CSV cell values using commas by default, and handling escaped separators.
protected
explode_values(string $value[, string $separator = ',' ]) : array<string|int, mixed>
Parameters
- $value : string
-
Value to explode.
- $separator : string = ','
-
Separator separating each value. Defaults to comma.
Return values
array<string|int, mixed> —explode_values_formatter()
Remove formatting and trim each value.
protected
explode_values_formatter(string $value) : string
Parameters
- $value : string
-
Value to format.
Return values
string —get_formatting_callback()
Get formatting callback.
protected
get_formatting_callback() : array<string|int, mixed>
Return values
array<string|int, mixed> —get_memory_limit()
Get memory limit
protected
get_memory_limit() : int
Return values
int —get_row_id()
Get a string to identify the row from parsed data.
protected
get_row_id(array<string|int, mixed> $parsed_data) : string
Parameters
- $parsed_data : array<string|int, mixed>
-
Parsed data.
Return values
string —memory_exceeded()
Memory exceeded
protected
memory_exceeded() : bool
Ensures the batch process never exceeds 90% of the maximum WordPress memory.
Return values
bool —process_item()
Process a single item and save.
protected
process_item(array<string|int, mixed> $data) : array<string|int, mixed>|WP_Error
Parameters
- $data : array<string|int, mixed>
-
Raw CSV data.
Tags
Return values
array<string|int, mixed>|WP_Error —read_file()
Read file.
protected
read_file() : mixed
Return values
mixed —remove_utf8_bom()
Remove UTF-8 BOM signature.
protected
remove_utf8_bom(string $string) : string
Parameters
- $string : string
-
String to handle.
Return values
string —set_mapped_keys()
Set file mapped keys.
protected
set_mapped_keys() : mixed
Return values
mixed —set_parsed_data()
Map and format raw data to known fields.
protected
set_parsed_data() : mixed
Return values
mixed —starts_with()
Check if strings starts with determined word.
protected
starts_with(string $haystack, string $needle) : bool
Parameters
- $haystack : string
-
Complete sentence.
- $needle : string
-
Excerpt.
Return values
bool —time_exceeded()
Time exceeded.
protected
time_exceeded() : bool
Ensures the batch never exceeds a sensible time limit. A timeout limit of 30s is common on shared hosting.
Return values
bool —unescape_data()
The exporter prepends a ' to escape fields that start with =, +, - or @.
protected
unescape_data(string $value) : string
Remove the prepended ' character preceding those characters.
Parameters
- $value : string
-
A string that may or may not have been escaped with '.