Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3037,7 +3037,7 @@ wp post-type get <post-type> [--field=<field>] [--fields=<fields>] [--format=<fo

**AVAILABLE FIELDS**

These fields will be displayed by default for each post type:
These fields will be displayed by default for the specified post type:

* name
* label
Expand All @@ -3049,7 +3049,9 @@ These fields will be displayed by default for each post type:
* cap
* supports

There are no optionally available fields.
These fields are optionally available:

* count

**EXAMPLES**

Expand Down Expand Up @@ -3101,7 +3103,9 @@ These fields will be displayed by default for each post type:
* public
* capability_type

There are no optionally available fields.
These fields are optionally available:

* count

**EXAMPLES**

Expand Down Expand Up @@ -3618,6 +3622,24 @@ wp taxonomy get <taxonomy> [--field=<field>] [--fields=<fields>] [--format=<form
- yaml
---

**AVAILABLE FIELDS**

These fields will be displayed by default for the specified taxonomy:

* name
* label
* description
* object_type
* show_tagcloud
* hierarchical
* public
* labels
* cap

These fields are optionally available:

* count

**EXAMPLES**

# Get details of `category` taxonomy.
Expand Down Expand Up @@ -3674,10 +3696,14 @@ These fields will be displayed by default for each term:
* name
* label
* description
* public
* object_type
* show_tagcloud
* hierarchical
* public

There are no optionally available fields.
These fields are optionally available:

* count

**EXAMPLES**

Expand Down
22 changes: 19 additions & 3 deletions features/post-type.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@ Feature: Manage WordPress post types
Scenario: Listing post types
When I run `wp post-type list --format=csv`
Then STDOUT should be CSV containing:
| name | label | description | hierarchical | public | capability_type |
| post | Posts | | | 1 | post |
| page | Pages | | 1 | 1 | page |
| name | label | description | hierarchical | public | capability_type |
| post | Posts | | | 1 | post |
| page | Pages | | 1 | 1 | page |

@require-wp-5.0
Scenario: Listing post types with count
When I run `wp post-type list --fields=name,count --format=csv`
Then STDOUT should be CSV containing:
| name | count |
| post | 1 |
| page | 2 |

Scenario: Get a post type
When I try `wp post-type get invalid-post-type`
Expand All @@ -31,3 +39,11 @@ Feature: Manage WordPress post types
"""
"title":true
"""

@require-wp-5.0
Scenario: Get a post type with count
When I try `wp post-type get page --fields=name,count`
Then STDOUT should be a table containing rows:
| Field | Value |
| name | page |
| count | 2 |
16 changes: 16 additions & 0 deletions features/taxonomy.feature
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ Feature: Manage WordPress taxonomies
| name | label | description | object_type | show_tagcloud | hierarchical | public |
| nav_menu | Navigation Menus | | nav_menu_item | | | |

@require-wp-5.0
Scenario: Listing taxonomies with counts
When I run `wp taxonomy list --fields=name,count --format=csv`
Then STDOUT should be CSV containing:
| name | count |
| category | 1 |
| post_tag | 0 |

Scenario: Get taxonomy
When I try `wp taxonomy get invalid-taxonomy`
Then STDERR should be:
Expand All @@ -30,3 +38,11 @@ Feature: Manage WordPress taxonomies
| name | category |
| object_type | ["post"] |
| label | Categories |

@require-wp-5.0
Scenario: Get taxonomy with count
When I run `wp taxonomy get category --fields=name,count`
Then STDOUT should be a table containing rows:
| Field | Value |
| name | category |
| count | 1 |
66 changes: 60 additions & 6 deletions src/Post_Type_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,37 @@ class Post_Type_Command extends WP_CLI_Command {
'capability_type',
);

/**
* Gets the post counts for each supplied post type.
*
* @param array $post_types Post types to fetch counts for.
* @return array Associative array of post counts keyed by post type.
*/
protected function get_counts( $post_types ) {
global $wpdb;

if ( count( $post_types ) <= 0 ) {
return [];
}

$query = $wpdb->prepare(
"SELECT `post_type`, COUNT(*) AS `count`
FROM $wpdb->posts
WHERE `post_type` IN (" . implode( ',', array_fill( 0, count( $post_types ), '%s' ) ) . ")
GROUP BY `post_type`",
$post_types
);
$counts = $wpdb->get_results( $query );

// Make sure there's a count for every item.
$counts = array_merge(
array_fill_keys( $post_types, 0 ),
wp_list_pluck( $counts, 'count', 'post_type' )
);

return $counts;
}

/**
* Lists registered post types.
*
Expand Down Expand Up @@ -71,7 +102,9 @@ class Post_Type_Command extends WP_CLI_Command {
* * public
* * capability_type
*
* There are no optionally available fields.
* These fields are optionally available:
*
* * count
*
* ## EXAMPLES
*
Expand Down Expand Up @@ -100,7 +133,18 @@ class Post_Type_Command extends WP_CLI_Command {
public function list_( $args, $assoc_args ) {
$formatter = $this->get_formatter( $assoc_args );

$types = get_post_types( $assoc_args, 'objects' );
$fields = $formatter->fields;
$types = get_post_types( $assoc_args, 'objects' );
$counts = [];

if ( count( $types ) > 0 && in_array( 'count', $fields, true ) ) {
$counts = $this->get_counts( wp_list_pluck( $types, 'name' ) );
}

$types = array_map( function( $type ) use ( $counts ) {
$type->count = isset( $counts[ $type->name ] ) ? $counts[ $type->name ] : 0;
return $type;
}, $types );

$formatter->display_items( $types );
}
Expand Down Expand Up @@ -132,7 +176,7 @@ public function list_( $args, $assoc_args ) {
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for each post type:
* These fields will be displayed by default for the specified post type:
*
* * name
* * label
Expand All @@ -144,7 +188,9 @@ public function list_( $args, $assoc_args ) {
* * cap
* * supports
*
* There are no optionally available fields.
* These fields are optionally available:
*
* * count
*
* ## EXAMPLES
*
Expand All @@ -169,6 +215,15 @@ public function get( $args, $assoc_args ) {
$assoc_args['fields'] = $default_fields;
}

$formatter = $this->get_formatter( $assoc_args );
$fields = $formatter->fields;
$count = 0;

if ( in_array( 'count', $fields, true ) ) {
$count = $this->get_counts( [ $post_type->name ] );
$count = $count[ $post_type->name ];
}

$data = array(
'name' => $post_type->name,
'label' => $post_type->label,
Expand All @@ -179,9 +234,8 @@ public function get( $args, $assoc_args ) {
'labels' => $post_type->labels,
'cap' => $post_type->cap,
'supports' => get_all_post_type_supports( $post_type->name ),
'count' => $count,
);

$formatter = $this->get_formatter( $assoc_args );
$formatter->display_item( $data );
}

Expand Down
78 changes: 73 additions & 5 deletions src/Taxonomy_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,37 @@ public function __construct() {
parent::__construct();
}

/**
* Gets the term counts for each supplied taxonomy.
*
* @param array $taxonomies Taxonomies to fetch counts for.
* @return array Associative array of term counts keyed by taxonomy.
*/
protected function get_counts( $taxonomies ) {
global $wpdb;

if ( count( $taxonomies ) <= 0 ) {
return [];
}

$query = $wpdb->prepare(
"SELECT `taxonomy`, COUNT(*) AS `count`
FROM $wpdb->term_taxonomy
WHERE `taxonomy` IN (" . implode( ',', array_fill( 0, count( $taxonomies ), '%s' ) ) . ")
GROUP BY `taxonomy`",
$taxonomies
);
$counts = $wpdb->get_results( $query );

// Make sure there's a count for every item.
$counts = array_merge(
array_fill_keys( $taxonomies, 0 ),
wp_list_pluck( $counts, 'count', 'taxonomy' )
);

return $counts;
}

/**
* Lists registered taxonomies.
*
Expand Down Expand Up @@ -77,10 +108,14 @@ public function __construct() {
* * name
* * label
* * description
* * public
* * object_type
* * show_tagcloud
* * hierarchical
* * public
*
* These fields are optionally available:
*
* There are no optionally available fields.
* * count
*
* ## EXAMPLES
*
Expand Down Expand Up @@ -112,10 +147,17 @@ public function list_( $args, $assoc_args ) {
$assoc_args['object_type'] = array( $assoc_args['object_type'] );
}

$fields = $formatter->fields;
$taxonomies = get_taxonomies( $assoc_args, 'objects' );
$counts = [];

if ( count( $taxonomies ) > 0 && in_array( 'count', $fields, true ) ) {
$counts = $this->get_counts( wp_list_pluck( $taxonomies, 'name' ) );
}

$taxonomies = array_map( function( $taxonomy ) {
$taxonomies = array_map( function( $taxonomy ) use ( $counts ) {
$taxonomy->object_type = implode( ', ', $taxonomy->object_type );
$taxonomy->count = isset( $counts[ $taxonomy->name ] ) ? $counts[ $taxonomy->name ] : 0;
return $taxonomy;
}, $taxonomies );

Expand Down Expand Up @@ -147,6 +189,24 @@ public function list_( $args, $assoc_args ) {
* - yaml
* ---
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for the specified taxonomy:
*
* * name
* * label
* * description
* * object_type
* * show_tagcloud
* * hierarchical
* * public
* * labels
* * cap
*
* These fields are optionally available:
*
* * count
*
* ## EXAMPLES
*
* # Get details of `category` taxonomy.
Expand Down Expand Up @@ -179,6 +239,15 @@ public function get( $args, $assoc_args ) {
$assoc_args['fields'] = $default_fields;
}

$formatter = $this->get_formatter( $assoc_args );
$fields = $formatter->fields;
$count = 0;

if ( in_array( 'count', $fields, true ) ) {
$count = $this->get_counts( [ $taxonomy->name ] );
$count = $count[ $taxonomy->name ];
}

$data = array(
'name' => $taxonomy->name,
'label' => $taxonomy->label,
Expand All @@ -189,9 +258,8 @@ public function get( $args, $assoc_args ) {
'public' => $taxonomy->public,
'labels' => $taxonomy->labels,
'cap' => $taxonomy->cap,
'count' => $count,
);

$formatter = $this->get_formatter( $assoc_args );
$formatter->display_item( $data );
}

Expand Down