1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
<?php
/**
* Manage terms.
*/
class Papi_CLI_Term_Command extends Papi_CLI_Command {
/**
* Get default fields for formatter.
*
* @return array
*/
protected function get_default_format_fields() {
return ['slug', 'type', 'has value', 'box'];
}
/**
* Get fields that exists on a term.
*
* ## OPTIONS
*
* <term>
* : Term ID
*
* [--field=<field>]
* : Instead of returning the whole term fields, returns the value of a single fields.
*
* [--fields=<fields>]
* : Get a specific subset of the term's fields.
*
* [--format=<format>]
* : Accepted values: table, json, csv. Default: table.
*
* ## AVAILABLE FIELDS
*
* These fields are available for get command:
*
* * slug
* * type
* * exists
*
* ## EXAMPLES
*
* wp papi term get 123 --format=json
*
* wp papi term get 123 --field=slug
*
* @param array $args
* @param array $assoc_args
*/
public function get( $args, $assoc_args ) {
try {
// Set query string that we need.
$_GET['meta_type'] = 'term';
// Get the taxonomy type that the term has.
$entry_type = papi_get_entry_type_by_meta_id( $args[0] );
if ( empty( $entry_type ) || $entry_type instanceof Papi_Taxonomy_Type === false ) {
WP_CLI::error( 'No taxonomy type exists on the term' );
}
$properties = [];
foreach ( $entry_type->get_boxes() as $box ) {
foreach ( $box->properties as $property ) {
$properties[] = [
'slug' => $property->get_slug( true ),
'type' => $property->type,
'has value' => $property->get_value() !== null ? 'true' : 'false',
'box' => $box->title
];
}
}
// Render types as a table.
$formatter = $this->get_formatter( $assoc_args );
$formatter->display_items( $properties );
} catch ( WC_CLI_Exception $e ) {
WP_CLI::error( $e->getMessage() );
}
}
}