From c561e95c7dd1fe3ad4a6e76a8008cbaf1aefffcc Mon Sep 17 00:00:00 2001 From: Atanas Angelov Date: Sat, 3 Nov 2018 02:31:58 +0200 Subject: [PATCH 1/2] Add count to post-type and taxonomy commands --- features/post-type.feature | 22 +++++++++-- features/taxonomy.feature | 16 ++++++++ src/Post_Type_Command.php | 66 +++++++++++++++++++++++++++++--- src/Taxonomy_Command.php | 78 +++++++++++++++++++++++++++++++++++--- 4 files changed, 168 insertions(+), 14 deletions(-) diff --git a/features/post-type.feature b/features/post-type.feature index bb0721e9b..56552c0fc 100644 --- a/features/post-type.feature +++ b/features/post-type.feature @@ -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` @@ -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 | diff --git a/features/taxonomy.feature b/features/taxonomy.feature index d7af6de75..d107ea57d 100644 --- a/features/taxonomy.feature +++ b/features/taxonomy.feature @@ -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: @@ -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 | diff --git a/src/Post_Type_Command.php b/src/Post_Type_Command.php index 4ff16febd..a80afdf0c 100644 --- a/src/Post_Type_Command.php +++ b/src/Post_Type_Command.php @@ -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. * @@ -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 * @@ -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 ); } @@ -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 @@ -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 * @@ -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, @@ -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 ); } diff --git a/src/Taxonomy_Command.php b/src/Taxonomy_Command.php index c1fb418bc..f473d03cb 100644 --- a/src/Taxonomy_Command.php +++ b/src/Taxonomy_Command.php @@ -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. * @@ -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 * @@ -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 ); @@ -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. @@ -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, @@ -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 ); } From 47f91a093180a0f0f21e464b2a8c6eb38e775e4d Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Sat, 16 Mar 2019 23:32:03 +0100 Subject: [PATCH 2/2] Regenerate README file --- README.md | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 001e0c759..6e6d84b7a 100644 --- a/README.md +++ b/README.md @@ -3037,7 +3037,7 @@ wp post-type get [--field=] [--fields=] [--format= [--field=] [--fields=] [--format=