Skip to content
This repository has been archived by the owner on Jan 15, 2019. It is now read-only.

Fix PHP Warning: Parameter must be an array or an object that implements Countable #661

Merged
merged 4 commits into from
Nov 29, 2018
Merged
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion header.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<?php twentynineteen_post_thumbnail(); ?>
<?php the_post(); ?>
<?php $discussion = ! is_page() && twentynineteen_can_show_post_thumbnail() ? twentynineteen_get_discussion_data() : null; ?>
<div class="<?php echo ( ! empty( $discussion ) && count( $discussion->responses ) > 0 ) ? 'entry-header has-discussion' : 'entry-header'; ?>">
<div class="<?php echo ( ! empty( $discussion ) && count( $discussion->authors ) > 0 ) ? 'entry-header has-discussion' : 'entry-header'; ?>">

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the intention is to count the number of responses, why change it to counting the number of authors instead? Just dumping count() did the trick for me locally. Since $discussion->responses returns a string value (which probably should be reviewed), wrapping it in absint() ensures we get an integer:

<div class="<?php echo ( ! empty( $discussion ) && absint( $discussion->responses ) > 0 ) ? 'entry-header has-discussion' : 'entry-header'; ?>">

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made your suggested change. Since you asked, the author count seemed more reliable at the time because $discussion->responses is derived from get_comment_count() and its return value can be filtered.

What can happen is another plugin, like muut, will filter the count to be zero (or even a placeholder to be filled later with JS). So there's a chance a post with comments will have a filtered value be zero, but that's probably an edge case. Worse case scenario is the CSS class won't be added there.

<?php get_template_part( 'template-parts/header/entry', 'header' ); ?>
</div><!-- .entry-header -->
<?php rewind_posts(); ?>
Expand Down