UpdateTerms
extends TaxonomyCLICommands
in package
UpdateTerms Class
Table of Contents
Properties
- $notices : array<string|int, array{type: string, message: string}>
- WP CLI notices.
- $upload_path : string
- Upload path
- $upload_url : string
- Upload URL
- $log_dir : string
- The directory where the log file will be created.
- $log_name : string
- The name of the log file to write to.
Methods
- add_notice() : void
- Adds a notice to be displayed at the end of the command.
- display_notices() : void
- Display the notices. The output is WP_CLI.
- get_notices() : array<string|int, array{type: string, message: string}>
- Get all notices.
- get_upload_path() : string
- Get the upload path.
- get_upload_url() : string
- Get the upload URL.
- invalid_taxonomy() : void|false
- Handles invalid taxonomy errors by logging and adding notices.
- log() : void
- Logs a message to a specified log file, creating the log directory if it does not exist.
- reset_notices() : void
- Reset all notices.
- set_log_name() : void
- Sets the name of the log file to write to.
- set_upload_path() : void
- Set the upload path.
- set_upload_url() : void
- Set the upload URL.
- update_terms() : void
- Updates or creates taxonomy terms with parent-child relationships.
- validate_post_type() : string|WP_Error
- Validates the given post type.
- validate_taxonomy() : string|WP_Error
- Validates the given taxonomy string.
- dry_run_result() : void
- Outputs a dry run message for a merge command.
- export_csv() : void
- Create and write to a CSV file
- get_log_file_path() : string
- Gets the full path to the log file.
- has_required_fields() : bool
- Checks if the given data array contains all required fields.
- is_term_valid() : WP_Term|false
- Validates a term by name or slug within a given taxonomy.
- validate_command_args() : bool
- Validate the number of command arguments.
- validate_headers() : bool
- Validates that the given array of CSV headers contains all required fields.
- process_csv() : void
- Processes a CSV file containing parent > children relationships and updates the terms in the specified taxonomy.
- process_single_term() : void
- Processes a single taxonomy term update command.
- process_terms() : void
- Process a set of terms with parent-child relationships.
Properties
$notices
WP CLI notices.
public
array<string|int, array{type: string, message: string}>
$notices
= array()
$upload_path
Upload path
public
string
$upload_path
$upload_url
Upload URL
public
string
$upload_url
$log_dir
The directory where the log file will be created.
protected
string
$log_dir
= BCM_UPLOADS_PATH
$log_name
The name of the log file to write to.
protected
string
$log_name
= 'default.log'
Methods
add_notice()
Adds a notice to be displayed at the end of the command.
public
add_notice([string $message = '' ][, string $type = 'log' ]) : void
Parameters
- $message : string = ''
-
The message to display.
- $type : string = 'log'
-
The type of notice. One of 'log', 'success', 'warning', 'error'. Default is 'log'.
display_notices()
Display the notices. The output is WP_CLI.
public
display_notices() : void
get_notices()
Get all notices.
public
get_notices() : array<string|int, array{type: string, message: string}>
Return values
array<string|int, array{type: string, message: string}> —The array of notices.
get_upload_path()
Get the upload path.
public
get_upload_path() : string
Return values
string —The full path.
get_upload_url()
Get the upload URL.
public
get_upload_url() : string
Return values
string —The full URL.
invalid_taxonomy()
Handles invalid taxonomy errors by logging and adding notices.
public
invalid_taxonomy(WP_Taxonomy|WP_Error $taxonomy[, int|null $row_num = null ]) : void|false
Parameters
- $taxonomy : WP_Taxonomy|WP_Error
-
The taxonomy or error object.
- $row_num : int|null = null
-
Optional. The row number for logging purposes. Defaults to null.
Return values
void|false —Returns false if row number is provided, otherwise void.
log()
Logs a message to a specified log file, creating the log directory if it does not exist.
public
log(string $message[, string $type = 'info' ][, string|null $log_name = null ]) : void
Parameters
- $message : string
-
The message to log.
- $type : string = 'info'
-
Optional. The type of log message. Default is 'info'.
- $log_name : string|null = null
-
Optional. The name of the log file to write to. If null, the default log file name is used.
reset_notices()
Reset all notices.
public
reset_notices() : void
set_log_name()
Sets the name of the log file to write to.
public
set_log_name(string $name) : void
Parameters
- $name : string
-
The name of the log file.
set_upload_path()
Set the upload path.
public
set_upload_path([string $path = '' ]) : void
If the path is a directory, it will create it if it does not exist.
Parameters
- $path : string = ''
-
The complete path to use.
set_upload_url()
Set the upload URL.
public
set_upload_url([string $url = '' ]) : void
Parameters
- $url : string = ''
-
The full URL to use.
update_terms()
Updates or creates taxonomy terms with parent-child relationships.
public
update_terms(array<string|int, string> $args, array<string, mixed> $assoc_args) : void
OPTIONS
[
[
[--file=
[--dry-run] : If set, no changes will be made.
[--log=
EXAMPLES
wp boomi taxonomies update_terms content-type 'News & Updates > Press Release, News'
wp boomi taxonomies update_terms content-type 'News & Updates > Press Release, News' --log=update-terms.log
wp boomi taxonomies update_terms --file=terms.csv --dry-run
wp boomi taxonomies update_terms --file=path/to/file.csv --log=log.txt
Parameters
- $args : array<string|int, string>
-
CLI positional arguments.
- $assoc_args : array<string, mixed>
-
CLI associative arguments.
Tags
validate_post_type()
Validates the given post type.
public
validate_post_type(string $post_type[, int|null $row_num = null ]) : string|WP_Error
Checks if the provided post type is a string and if it exists. Returns a WP_Error if the validation fails.
Parameters
- $post_type : string
-
The post type to validate.
- $row_num : int|null = null
-
The row number in the CSV file (optional).
Return values
string|WP_Error —The post type if valid, or a WP_Error on failure.
validate_taxonomy()
Validates the given taxonomy string.
public
validate_taxonomy(string $taxonomy[, int|null $row_num = null ]) : string|WP_Error
Checks if the provided taxonomy is a string and if it exists. Returns a WP_Error if the taxonomy is invalid.
Parameters
- $taxonomy : string
-
The taxonomy to validate.
- $row_num : int|null = null
-
The row number in the CSV file (optional).
Return values
string|WP_Error —Returns the taxonomy if valid, otherwise a WP_Error object.
dry_run_result()
Outputs a dry run message for a merge command.
protected
dry_run_result(string $taxonomy, array<string|int, string> $from_terms, string $to_term, int $row_num) : void
Parameters
- $taxonomy : string
-
The taxonomy.
- $from_terms : array<string|int, string>
-
The terms to merge.
- $to_term : string
-
The term to merge into.
- $row_num : int
-
The row number.
export_csv()
Create and write to a CSV file
protected
export_csv(string $filename, array<int|string, mixed> $rows[, array<int|string, string> $headers = array() ]) : void
Parameters
- $filename : string
-
the full name of the file.
- $rows : array<int|string, mixed>
-
array of data.
- $headers : array<int|string, string> = array()
-
(default: array()).
get_log_file_path()
Gets the full path to the log file.
protected
get_log_file_path(string $log_name) : string
Parameters
- $log_name : string
-
The log file name.
Return values
string —The full path to the log file.
has_required_fields()
Checks if the given data array contains all required fields.
protected
has_required_fields(array<string, mixed> $data, array<string|int, string> $required[, int $row_num = 0 ]) : bool
Parameters
- $data : array<string, mixed>
-
The data array to check.
- $required : array<string|int, string>
-
The list of required field keys.
- $row_num : int = 0
-
The row number for logging purposes. Defaults to 0.
Return values
bool —Returns true if all required fields are present, false otherwise.
is_term_valid()
Validates a term by name or slug within a given taxonomy.
protected
is_term_valid(string $term_name, string $taxonomy[, int $row_num = 0 ]) : WP_Term|false
Parameters
- $term_name : string
-
The term name or slug.
- $taxonomy : string
-
The taxonomy to look in.
- $row_num : int = 0
-
The row number in the CSV file.
Return values
WP_Term|false —If the term is valid, returns the term object. Otherwise, returns false.
validate_command_args()
Validate the number of command arguments.
protected
validate_command_args(array<string|int, string> $args[, int $min_args = 0 ][, int $max_args = 0 ]) : bool
Parameters
- $args : array<string|int, string>
-
The command arguments.
- $min_args : int = 0
-
The minimum number of arguments allowed. Default 0.
- $max_args : int = 0
-
The maximum number of arguments allowed. Default 0.
Return values
bool —True if valid, false otherwise.
validate_headers()
Validates that the given array of CSV headers contains all required fields.
protected
validate_headers(array<string|int, string> $headers, array<string|int, string> $required) : bool
Parameters
- $headers : array<string|int, string>
-
The array of CSV headers.
- $required : array<string|int, string>
-
The list of required field keys.
Return values
bool —Returns true if all required fields are present, false otherwise.
process_csv()
Processes a CSV file containing parent > children relationships and updates the terms in the specified taxonomy.
private
process_csv(string $file, bool $dry_run) : void
Parameters
- $file : string
-
Path to the CSV file.
- $dry_run : bool
-
If set, no changes will be made.
process_single_term()
Processes a single taxonomy term update command.
private
process_single_term(array<string|int, string> $args, bool $dry_run) : void
Validates the taxonomy and command arguments, then parses the input for parent-child relationships to generate mappings. If the input format is invalid, adds a notice and logs the error. If valid, it processes the terms based on the mappings.
Parameters
- $args : array<string|int, string>
-
CLI arguments including taxonomy and term relationships.
- $dry_run : bool
-
If true, simulates the update process without making changes.
process_terms()
Process a set of terms with parent-child relationships.
private
process_terms(array<string|int, array{parent: string, children: string[]}> $mappings, string $taxonomy, bool $dry_run) : void
Parameters
- $mappings : array<string|int, array{parent: string, children: string[]}>
-
Set of term sets with parent and children.
- $taxonomy : string
-
The taxonomy to update.
- $dry_run : bool
-
If set, no changes will be made.