From d3a198df34b4fe873285ed383c194ea78e022a78 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Tue, 3 Jul 2018 10:28:39 +0000 Subject: [PATCH] Taxonomy: Introduce `is_taxonomy_viewable()`. This utility function allows for easy detection whether terms for a taxonomy are considered publicly viewable. Props andizer. Fixes #44466. git-svn-id: https://develop.svn.wordpress.org/trunk@43386 602fd350-edb4-49c9-b593-d223f7449a82 --- .../includes/class-wp-terms-list-table.php | 2 +- src/wp-includes/admin-bar.php | 2 +- src/wp-includes/taxonomy.php | 19 ++++++++ .../tests/taxonomy/isTaxonomyViewable.php | 44 +++++++++++++++++++ 4 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 tests/phpunit/tests/taxonomy/isTaxonomyViewable.php diff --git a/src/wp-admin/includes/class-wp-terms-list-table.php b/src/wp-admin/includes/class-wp-terms-list-table.php index f7dacdd13a..0afe832607 100644 --- a/src/wp-admin/includes/class-wp-terms-list-table.php +++ b/src/wp-admin/includes/class-wp-terms-list-table.php @@ -470,7 +470,7 @@ class WP_Terms_List_Table extends WP_List_Table { __( 'Delete' ) ); } - if ( $tax->public ) { + if ( is_taxonomy_viewable( $tax ) ) { $actions['view'] = sprintf( '%s', get_term_link( $tag ), diff --git a/src/wp-includes/admin-bar.php b/src/wp-includes/admin-bar.php index c21916e4f9..70cca8d2da 100644 --- a/src/wp-includes/admin-bar.php +++ b/src/wp-includes/admin-bar.php @@ -728,7 +728,7 @@ function wp_admin_bar_edit_menu( $wp_admin_bar ) { } elseif ( 'term' == $current_screen->base && isset( $tag ) && is_object( $tag ) && ! is_wp_error( $tag ) && ( $tax = get_taxonomy( $tag->taxonomy ) ) - && $tax->public ) { + && is_taxonomy_viewable( $tax ) ) { $wp_admin_bar->add_menu( array( 'id' => 'view', diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 2df5ac6b41..3f9b960538 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -4548,3 +4548,22 @@ function wp_check_term_hierarchy_for_loops( $parent, $term_id, $taxonomy ) { return $parent; } + +/** + * Determines whether a taxonomy is considered "viewable". + * + * @since 5.0.0 + * + * @param string|WP_Taxonomy $taxonomy Taxonomy name or object. + * @return bool Whether the taxonomy should be considered viewable. + */ +function is_taxonomy_viewable( $taxonomy ) { + if ( is_scalar( $taxonomy ) ) { + $taxonomy = get_taxonomy( $taxonomy ); + if ( ! $taxonomy ) { + return false; + } + } + + return $taxonomy->publicly_queryable; +} diff --git a/tests/phpunit/tests/taxonomy/isTaxonomyViewable.php b/tests/phpunit/tests/taxonomy/isTaxonomyViewable.php new file mode 100644 index 0000000000..7d5c4222f5 --- /dev/null +++ b/tests/phpunit/tests/taxonomy/isTaxonomyViewable.php @@ -0,0 +1,44 @@ + true ) ); + register_taxonomy( 'wptests_tax_non_viewable', 'wptests_pt', array( 'publicly_queryable' => false ) ); + } + + /** + * @ticket 44466 + */ + public function test_is_taxonomy_viewable_for_querable_taxonomy() { + $this->assertTrue( is_taxonomy_viewable( 'wptests_tax_viewable' ) ); + } + + /** + * @ticket 44466 + */ + public function test_is_taxonomy_viewable_for_non_querable_taxonomy() { + $this->assertFalse( is_taxonomy_viewable( 'wptests_tax_non_viewable' ) ); + } + + /** + * @ticket 44466 + */ + public function test_is_taxonomy_viewable_for_non_existing_taxonomy() { + $this->assertFalse( is_taxonomy_viewable( 'wptests_tax_non_existing' ) ); + } + + /** + * @ticket 44466 + */ + public function test_is_taxonomy_viewable_with_object_given() { + $taxonomy = get_taxonomy( 'wptests_tax_viewable' ); + + $this->assertTrue( is_taxonomy_viewable( $taxonomy ) ); + } +}