WordPress.org

Make WordPress Core

Opened 8 years ago

Closed 8 years ago

#27413 closed defect (bug) (fixed)

PHP warnings generated by wp_generate_tag_cloud

Reported by: tomdxw Owned by: nacin
Milestone: 3.9 Priority: normal
Severity: normal Version: 2.8
Component: Taxonomy Keywords: has-patch
Focuses: Cc:

Description (last modified by SergeyBiryukov)

PHP Warning: min(): Array must contain at least one element in /var/vhost/xxxx/wp-includes/category-template.php on line 658
PHP Warning: max(): Array must contain at least one element in /var/vhost/xxxx/wp-includes/category-template.php on line 659

I'm not sure how to reproduce the bug but it can be fixed easily by replacing min( $counts ) with count( $counts ) > 0 ? min( $counts ) : 0 and replacing max( $counts ) with (count( $counts ) > 0 ? max( $counts ) : 0). Or by checking whether $counts is empty and returning early.

Attachments (2)

27413.patch (615 bytes) - added by desaiuditd 8 years ago.
27413.2.patch (749 bytes) - added by SergeyBiryukov 8 years ago.

Download all attachments as: .zip

Change History (8)

@desaiuditd
8 years ago

#1 @desaiuditd
8 years ago

  • Keywords has-patch dev-feedback needs-testing added

Added patch for the issue reported.
Review needed.
27413.patch

#2 @SergeyBiryukov
8 years ago

  • Component changed from General to Taxonomy
  • Description modified (diff)
  • Keywords close added; dev-feedback needs-testing removed

Looks like the only way to trigger these warnings is to return an empty array in tag_cloud_sort filter:

add_filter( 'tag_cloud_sort', '__return_empty_array' );

This looks like a developer error, so the developer should see the warning.

If the goal is to remove the output, wp_generate_tag_cloud filter should be used instead:
tags/3.8.1/src/wp-includes/category-template.php#L694

#3 @desaiuditd
8 years ago

Yeah, that's true. This is the outcome of an unexpected behavior. Though I think, it should be handled because even the developers should not be put into confusion with such unwanted warnings/errors. Even if someone is passing an empty array; that should not affect the default workflow i.e., generate tag cloud if there are tags or do not generate tag cloud if empty array is passed

#4 @SergeyBiryukov
8 years ago

  • Keywords close removed
  • Milestone changed from Awaiting Review to 3.9
  • Version changed from 3.8.1 to 2.8

Introduced in [11037].

I guess we could check the filtered value and return if it's empty, see 27413.2.patch.

Also noticed that the function doesn't have a consistent return value, see #27487.

#5 @desaiuditd
8 years ago

Fair enough. +1

#6 @nacin
8 years ago

  • Owner set to nacin
  • Resolution set to fixed
  • Status changed from new to closed

In 27709:

Gracefully fail in wp_generate_tag_cloud() when emptiness is returned from the tag_cloud_sort filter.

props SergeyBiryukov.
fixes #27413.

Note: See TracTickets for help on using tickets.