Make WordPress Core

Opened 3 months ago

Closed 3 months ago

#64864 closed defect (bug) (fixed)

previous_posts(): PHP 8.1 deprecation notice

Reported by: dd32's profile dd32 Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 7.0 Priority: low
Severity: trivial Version:
Component: Themes Keywords: php81 has-patch has-unit-tests
Focuses: coding-standards Cc:

Description

PHP 8.1 deprecation:

( ! ) Deprecated: ltrim(): Passing null to parameter #1 ($string) of type string is deprecated in wp-includes/formatting.php on line 4487

This is caused by calling previous_posts( $display = FALSE ) devref on a singular page/post, as done by at least one plugin.

get_previous_posts_page_link() returns null, which is then passed to esc_url() in prevoous_posts().

Change History (3)

This ticket was mentioned in PR #11264 on WordPress/wordpress-develop by @alexodiy.


3 months ago
#1

  • Keywords has-patch has-unit-tests added

This adds a null guard to previous_posts(), matching the existing pattern used in next_posts()

Without this check, calling previous_posts() on a singular view can pass null to esc_url(), which in turn triggers an ltrim() deprecation notice on PHP 8.1 and newer

Props @dd32
Fixes #64864

#2 @SergeyBiryukov
3 months ago

  • Milestone changed from Future Release to 7.0

#3 @SergeyBiryukov
3 months ago

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

In 62034:

Code Modernization: Fix "passing null to non-nullable" deprecation from previous_posts().

The esc_url() function expects to a string for $url parameter. There is no input validation within that function. The function contains a ltrim() which also expects a string. Passing null to this parameter results in Deprecated: ltrim(): Passing null to parameter #1 ($string) of type string is deprecated notice on PHP 8.1+.

Tracing the stack back, a null is being passed to it within previous_posts() when get_previous_posts_page_link() returns null (it can return a string or null).

On PHP 7.0 to PHP 8.x, an empty string is returned from esc_url() when null is passed to it. The change in this changeset avoids the deprecation notice by not invoking esc_url() when get_previous_posts_page_link() returns null and instead sets the $output to an empty string, thus maintaining the same behavior as before (minus the deprecation notice).

Adds a test to validate an empty string is returned and the absence of the deprecation (when running on PHP 8.1+).

Follow-up to [9632], [11383], [56740].

Props dd32, alexodiy.
Fixes #64864.

Note: See TracTickets for help on using tickets.