Skip to content

Commit

Permalink
Code Modernization: Fix "passing null to non-nullable" deprecation fr…
Browse files Browse the repository at this point in the history
…om next_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 `next_posts()` when `get_next_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_next_posts_page_link()` returns `null` and instead sets the `$output` to an empty string, thus maintain 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 [11383], [9632].

Props codersantosh, nihar007, hellofromTonya, mukesh27, oglekler, rajinsharwar.
Fixes #59154.

git-svn-id: https://develop.svn.wordpress.org/trunk@56740 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
hellofromtonya committed Sep 28, 2023
1 parent e3a612a commit f0a1369
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/wp-includes/link-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -2511,7 +2511,8 @@ function get_next_posts_page_link( $max_page = 0 ) {
* @return string|void The link URL for next posts page if `$display = false`.
*/
function next_posts( $max_page = 0, $display = true ) {
$output = esc_url( get_next_posts_page_link( $max_page ) );
$link = get_next_posts_page_link( $max_page );
$output = $link ? esc_url( $link ) : '';

if ( $display ) {
echo $output;
Expand Down
41 changes: 41 additions & 0 deletions tests/phpunit/tests/link/nextPosts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* Tests the `next_posts()` function.
*
* @since 6.4.0
*
* @group link
*
* @covers ::next_posts
*/
class Tests_Link_NextPosts extends WP_UnitTestCase {

/**
* Creates posts before any tests run.
*
* @param WP_UnitTest_Factory $factory
*/
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
global $wp_query, $paged;

$factory->post->create_many( 3 );
$paged = 2;
$wp_query = new WP_Query(
array(
'post_type' => 'post',
'posts_per_page' => 1,
'paged' => $paged,
)
);
}

/**
* The absence of a deprecation notice on PHP 8.1+ also shows that the issue is resolved.
*
* @ticket 59154
*/
public function test_should_return_empty_string_when_no_next_posts_page_link() {
$this->assertSame( '', next_posts( 1, false ) );
}
}

0 comments on commit f0a1369

Please sign in to comment.