Skip to content

Commit f0a1369

Browse files
Code Modernization: Fix "passing null to non-nullable" deprecation from 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
1 parent e3a612a commit f0a1369

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

src/wp-includes/link-template.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2511,7 +2511,8 @@ function get_next_posts_page_link( $max_page = 0 ) {
25112511
* @return string|void The link URL for next posts page if `$display = false`.
25122512
*/
25132513
function next_posts( $max_page = 0, $display = true ) {
2514-
$output = esc_url( get_next_posts_page_link( $max_page ) );
2514+
$link = get_next_posts_page_link( $max_page );
2515+
$output = $link ? esc_url( $link ) : '';
25152516

25162517
if ( $display ) {
25172518
echo $output;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* Tests the `next_posts()` function.
5+
*
6+
* @since 6.4.0
7+
*
8+
* @group link
9+
*
10+
* @covers ::next_posts
11+
*/
12+
class Tests_Link_NextPosts extends WP_UnitTestCase {
13+
14+
/**
15+
* Creates posts before any tests run.
16+
*
17+
* @param WP_UnitTest_Factory $factory
18+
*/
19+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
20+
global $wp_query, $paged;
21+
22+
$factory->post->create_many( 3 );
23+
$paged = 2;
24+
$wp_query = new WP_Query(
25+
array(
26+
'post_type' => 'post',
27+
'posts_per_page' => 1,
28+
'paged' => $paged,
29+
)
30+
);
31+
}
32+
33+
/**
34+
* The absence of a deprecation notice on PHP 8.1+ also shows that the issue is resolved.
35+
*
36+
* @ticket 59154
37+
*/
38+
public function test_should_return_empty_string_when_no_next_posts_page_link() {
39+
$this->assertSame( '', next_posts( 1, false ) );
40+
}
41+
}

0 commit comments

Comments
 (0)