Skip to content

Commit

Permalink
(fix) Handle empty array return from get_post_taxonomies()
Browse files Browse the repository at this point in the history
Before: the `::taxonomies()` method was configured to allow for a null
return from `get_post_taxonomies()`. However, it will always return an
array, it's just that the array will be empty if there are no post
taxonomies.

Now: we handle an empty array return.

See: https://developer.wordpress.org/reference/functions/get_post_taxonomies/
  • Loading branch information
RobjS committed Feb 3, 2025
1 parent d31aebd commit 8dd3f16
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 5 deletions.
2 changes: 1 addition & 1 deletion spec/page.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
});
context('post has no taxonomies', function () {
it('returns [\'none\']', function () {
allow('get_post_taxonomies')->toBeCalled()->andReturn(null);
allow('get_post_taxonomies')->toBeCalled()->andReturn([]);
expect($this->page->taxonomies())->toEqual(['none']);
});
});
Expand Down
5 changes: 1 addition & 4 deletions src/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ public function postType(): string

public function taxonomies(): array
{
// Note: this implementation is actually buggy
// get_post_taxonomies will never return null, only an empty array
// Spotted in a refactor, will be fixed in a separate commit
return get_post_taxonomies() ?? ['none'];
return get_post_taxonomies() ?: ['none'];
}

public function templateName(): string
Expand Down

0 comments on commit 8dd3f16

Please # to comment.