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 posts.
*/
class Papi_CLI_Post_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 post.
*
* ## OPTIONS
*
* <post>
* : Post ID
*
* [--field=<field>]
* : Instead of returning the whole post fields, returns the value of a single fields.
*
* [--fields=<fields>]
* : Get a specific subset of the post'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 post get 123 --format=json
*
* wp papi post 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['post'] = $args[0];
// Get the page type that the post has.
$entry_type = papi_get_entry_type_by_meta_id( $args[0] );
if ( empty( $entry_type ) || $entry_type instanceof Papi_Page_Type === false ) {
WP_CLI::error( 'No page type exists on the post' );
}
$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() );
}
}
}