Skip to content

Commit d7d08b0

Browse files
authored
Merge pull request #256 from BhargavBhandari90/issue-181
Add strict/no-strict mode for taxonomy list
2 parents 1e0ec2c + ffedeea commit d7d08b0

File tree

2 files changed

+93
-2
lines changed

2 files changed

+93
-2
lines changed

features/taxonomy.feature

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,83 @@ Feature: Manage WordPress taxonomies
4646
| Field | Value |
4747
| name | category |
4848
| count | 1 |
49+
50+
@require-wp-5.1
51+
Scenario: Listing taxonomies with strict/no-strict mode
52+
Given a WP installation
53+
And a wp-content/mu-plugins/test-taxonomy-list.php file:
54+
"""
55+
<?php
56+
// Plugin Name: Test Taxonomy Strict/No-Strict Mode
57+
58+
add_action( 'init', function() {
59+
$args = array(
60+
'hierarchical' => true,
61+
'show_ui' => true,
62+
'show_admin_column' => true,
63+
'update_count_callback' => '_update_post_term_count',
64+
'query_var' => true,
65+
'labels' => array(
66+
'name' => _x( 'Genres', 'taxonomy general name', 'textdomain' ),
67+
),
68+
69+
);
70+
71+
register_taxonomy( 'genres', array( 'post','page' ), $args );
72+
} );
73+
"""
74+
75+
When I run `wp taxonomy list --object_type=post --strict`
76+
Then STDOUT should be a table containing rows:
77+
| name | label | description | object_type | show_tagcloud | hierarchical | public |
78+
| category | Categories | | post | 1 | 1 | 1 |
79+
| post_tag | Tags | | post | 1 | | 1 |
80+
| post_format | Formats | | post | | | 1 |
81+
82+
When I run `wp taxonomy list --object_type=post --no-strict`
83+
Then STDOUT should be a table containing rows:
84+
| name | label | description | object_type | show_tagcloud | hierarchical | public |
85+
| category | Categories | | post | 1 | 1 | 1 |
86+
| post_tag | Tags | | post | 1 | | 1 |
87+
| post_format | Formats | | post | | | 1 |
88+
| genres | Genres | | post, page | 1 | 1 | 1 |
89+
90+
@less-than-wp-5.1
91+
Scenario: Listing taxonomies with strict/no-strict mode (for WP < 5.1)
92+
Given a WP installation
93+
And a wp-content/mu-plugins/test-taxonomy-list.php file:
94+
"""
95+
<?php
96+
// Plugin Name: Test Taxonomy Strict/No-Strict Mode
97+
98+
add_action( 'init', function() {
99+
$args = array(
100+
'hierarchical' => true,
101+
'show_ui' => true,
102+
'show_admin_column' => true,
103+
'update_count_callback' => '_update_post_term_count',
104+
'query_var' => true,
105+
'labels' => array(
106+
'name' => _x( 'Genres', 'taxonomy general name', 'textdomain' ),
107+
),
108+
109+
);
110+
111+
register_taxonomy( 'genres', array( 'post','page' ), $args );
112+
} );
113+
"""
114+
115+
When I run `wp taxonomy list --object_type=post --strict`
116+
Then STDOUT should be a table containing rows:
117+
| name | label | description | object_type | show_tagcloud | hierarchical | public |
118+
| category | Categories | | post | 1 | 1 | 1 |
119+
| post_tag | Tags | | post | 1 | | 1 |
120+
| post_format | Format | | post | | | 1 |
121+
122+
When I run `wp taxonomy list --object_type=post --no-strict`
123+
Then STDOUT should be a table containing rows:
124+
| name | label | description | object_type | show_tagcloud | hierarchical | public |
125+
| category | Categories | | post | 1 | 1 | 1 |
126+
| post_tag | Tags | | post | 1 | | 1 |
127+
| post_format | Format | | post | | | 1 |
128+
| genres | Genres | | post, page | 1 | 1 | 1 |

src/Taxonomy_Command.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,24 @@ protected function get_counts( $taxonomies ) {
148148
public function list_( $args, $assoc_args ) {
149149
$formatter = $this->get_formatter( $assoc_args );
150150

151+
// Check if it's strict mode or not.
152+
$strict = Utils\get_flag_value( $assoc_args, 'strict', false );
153+
154+
unset( $assoc_args['strict'] );
155+
151156
if ( isset( $assoc_args['object_type'] ) ) {
152157
$assoc_args['object_type'] = array( $assoc_args['object_type'] );
158+
$taxonomy_object = $assoc_args['object_type'];
159+
} else {
160+
$taxonomy_object = get_post_types();
153161
}
154162

155163
$fields = $formatter->fields;
156-
$taxonomies = get_taxonomies( $assoc_args, 'objects' );
157-
$counts = [];
164+
$taxonomies = ( isset( $taxonomy_object ) && ! $strict )
165+
? get_object_taxonomies( $taxonomy_object, 'objects' )
166+
: get_taxonomies( $assoc_args, 'objects' );
167+
168+
$counts = [];
158169

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

0 commit comments

Comments
 (0)