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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
<?php
/**
* Manage types.
*/
class Papi_CLI_Type_Command extends Papi_CLI_Command {
/**
* Get default fields for formatter.
*
* @return array
*/
protected function get_default_format_fields() {
return ['name', 'id', 'meta type', 'meta type value', 'template', 'db count', 'type'];
}
/**
* Get meta type value.
*
* @param Papi_Entry_Type $entry_type
*
* @return string
*/
protected function get_meta_type_value( $entry_type ) {
if ( in_array( $entry_type->get_type(), ['attachment'] ) ) {
return $entry_type->get_type();
}
switch ( papi_get_meta_type( $entry_type->get_type() ) ) {
case 'post':
return implode( ', ', $entry_type->post_type );
case 'term':
return implode( ', ', $entry_type->taxonomy );
default:
return 'n/a';
}
}
/**
* List Papi types.
*
* ## Options
*
* [--<field>=<value>]
* : Filter types based on type property.
*
* [--field=<field>]
* : Prints the value of a single field for each type.
*
* [--fields=<fields>]
* : Limit the output to specific type fields.
*
* [--format=<format>]
* : Acceptec values: table, csv, json, count, ids. Default: table.
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for each type:
*
* * name
* * id
* * post_type
* * template
* * number_of_pages
* * type
*
* Not all fields exists on a Papi type so some fields will have `n/a`
* as value when no value can be displayed.
*
* ## EXAMPLES
*
* wp papi type list
*
* @subcommand list
*/
public function list_( $args, $assoc_args ) {
// Get all entry types.
$entry_types = papi_get_all_entry_types();
if ( empty( $entry_types ) ) {
WP_CLI::error( 'No Papi types exists.' );
}
// Create type item with the fields that
// will be displayed.
$entry_types = array_map( function( $entry_type ) {
return [
'id' => $entry_type->get_id(),
'name' => $entry_type->name,
'meta type' => papi_is_page_type( $entry_type ) ? 'post' : $entry_type->type,
'meta type value' => $this->get_meta_type_value( $entry_type ),
'template' => empty( $entry_type->template ) ? 'n/a' : $entry_type->template,
'type' => $entry_type->get_type(),
'db count' => $entry_type->type === 'option' ? 'n/a' : papi_get_entry_type_count( $entry_type )
];
}, $entry_types );
// Render types as a table.
$formatter = $this->get_formatter( $assoc_args );
$formatter->display_items( $entry_types );
}
}