CSV_Importer

This class is the controller for the CSV Import

Tags
author

Barn2 Plugins support@barn2.com

license

GPL-3.0

copyright

Barn2 Media Ltd

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_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_position

The file position after the last read.

protected int $file_position = 0
Tags

$file_positions

Raw data.

protected array<string|int, mixed> $file_positions = []
Tags

$mapped_keys

Mapped keys - CSV headers.

protected array<string|int, mixed> $mapped_keys = []
Tags

$params

Importer parameters.

protected array<string|int, mixed> $params = []
Tags

$parsed_data

Parsed data.

protected array<string|int, mixed> $parsed_data = []
Tags

$parsing_raw_data_index

Tracks current row being parsed.

protected int $parsing_raw_data_index = 0
Tags

$raw_data

Raw data.

protected array<string|int, mixed> $raw_data = []
Tags

$raw_keys

Raw keys - CSV raw headers.

protected array<string|int, mixed> $raw_keys = []
Tags

$start_time

Start time of current import.

protected int $start_time = 0

(default value: 0)

Tags

$taxonomies

A list of taxonomies registered to the Document post type.

protected array<string|int, mixed> $taxonomies = []
Tags

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.

Tags
Return values
mixed

get_file_position()

Get file pointer position from the last read.

public get_file_position() : int
Tags
Return values
int

get_mapped_keys()

Get file mapped headers.

public get_mapped_keys() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>

get_params()

Get importer parameters.

public get_params() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>

get_parsed_data()

Get parsed data.

public get_parsed_data() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>

get_percent_complete()

Get file pointer position as a percentage of file size.

public get_percent_complete() : int
Tags
Return values
int

get_raw_data()

Get raw data.

public get_raw_data() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>

get_raw_keys()

Get file raw headers.

public get_raw_keys() : array<string|int, mixed>
Tags
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.

Tags
Return values
WP_Taxonomy

import()

Process importer.

public import() : array<string|int, mixed>
Tags
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.

Tags
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.

Tags
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.

Tags
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.

Tags
Return values
string|null

parse_description_field()

Parse a description value field

public parse_description_field(string $description) : string
Parameters
$description : string

field value.

Tags
Return values
string

parse_float_field()

Parse a float value field.

public parse_float_field(string $value) : float|string
Parameters
$value : string

Field value.

Tags
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.

Tags
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.

Tags
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.

Tags
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.

Tags
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.

Tags
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.

Tags
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.

Tags
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.

Tags
Return values
array<string|int, mixed>

parse_url_field()

Parse the URL field.

public parse_url_field(mixed $value) : string|false
Parameters
$value : mixed
Tags
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.

Tags
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.

Tags
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.

Tags
Return values
string

get_formatting_callback()

Get formatting callback.

protected get_formatting_callback() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>

get_memory_limit()

Get memory limit

protected get_memory_limit() : int
Tags
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.

Tags
Return values
string

memory_exceeded()

Memory exceeded

protected memory_exceeded() : bool

Ensures the batch process never exceeds 90% of the maximum WordPress memory.

Tags
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
throws
Exception

If item cannot be processed.

Return values
array<string|int, mixed>|WP_Error

read_file()

Read file.

protected read_file() : mixed
Tags
Return values
mixed

remove_utf8_bom()

Remove UTF-8 BOM signature.

protected remove_utf8_bom(string $string) : string
Parameters
$string : string

String to handle.

Tags
Return values
string

set_mapped_keys()

Set file mapped keys.

protected set_mapped_keys() : mixed
Tags
Return values
mixed

set_parsed_data()

Map and format raw data to known fields.

protected set_parsed_data() : mixed
Tags
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.

Tags
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.

Tags
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 '.

Tags
Return values
string

Search results