WordPress.org

Make WordPress Core

Opened 23 months ago

Closed 9 months ago

Last modified 9 months ago

#42742 closed enhancement (fixed)

Add a has_errors() method to WP_Error

Reported by: DrewAPicture Owned by: robdxw
Milestone: 5.1 Priority: normal
Severity: normal Version:
Component: General Keywords: has-unit-tests has-dev-note
Focuses: Cc:
PR Number:

Description

We should add a method to easily check whether a WP_Error object contains any errors.

There isn't any one standard way to perform such an operation, and in fact, a cursory search of core yielded a variety of methods for checking whether there are any errors in the object, but nothing really consistent:

  • Checking empty() on the $errors property
  • Checking if the count from get_error_codes() is greater than 0
  • Checking if the value of get_error_code() is an empty string

My proposal would be to introduce a boolean has_errors() method, or perhaps empty() that simply checks whether the $errors property is empty or not.

Attachments (3)

42742-wp-error-tests.diff (26.8 KB) - added by DrewAPicture 23 months ago.
WP_Error full tests coverage
42742_wp_error_has_errors_method.diff (1.7 KB) - added by robdxw 21 months ago.
Add has_errors() method and tests
42742.diff (1.6 KB) - added by DrewAPicture 21 months ago.

Download all attachments as: .zip

Change History (12)

@DrewAPicture
23 months ago

WP_Error full tests coverage

#1 @DrewAPicture
23 months ago

In 42255:

General: Add complete test coverage for WP_Error.

See #42742.

@robdxw
21 months ago

Add has_errors() method and tests

#2 @robdxw
21 months ago

I've submitted a patch adding the has_errors() method & tests. This is my first patch, so please let me know if anything needs changing.

#3 @SergeyBiryukov
21 months ago

  • Milestone changed from Awaiting Review to 5.0

Hi @robdxw, thanks for the patch! It looks good to me.

As a next step, we could update all the instances in WordPress core that currently check if a WP_Error object contains any errors in one way or another (as outlined in the ticket description) to use the new method instead.

#4 @DrewAPicture
21 months ago

In 42646:

Tests: Simplify @covers annotations for WP_Error test coverage. Also fix a typo in a test method name.

See #42742.

@DrewAPicture
21 months ago

#5 @DrewAPicture
21 months ago

  • Keywords has-patch has-unit-tests added; needs-patch needs-unit-tests removed
  • Owner set to robdxw
  • Status changed from new to assigned

42742.diff just updates @robdxw's latest patch to fix the simplified @covers annotations introduced in [42646].

Also assigns the ticket to @robdxw to mark the good-first-bug as "claimed".

#6 @SergeyBiryukov
20 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 42761:

General: Introduce WP_Error::has_errors() method and use it where appropriate.

Props robdxw, DrewAPicture, SergeyBiryukov.
Fixes #42742.

#7 @johnbillion
12 months ago

  • Keywords needs-patch added; good-first-bug has-patch removed
  • Milestone changed from 5.0 to 5.1
  • Resolution fixed deleted
  • Status changed from closed to reopened

@since needs updating

#8 @desrosj
9 months ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 44527:

Docs: Update since annotations WP_Error::has_errors()

Introduced in [42761].

Fixes #42742.

#9 @desrosj
9 months ago

  • Keywords has-dev-note added; needs-patch removed
Note: See TracTickets for help on using tickets.