From 7a6bff44fcae987548e55542e2f4db3c726efe99 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sun, 25 Jun 2023 10:48:28 +0000 Subject: [PATCH] Code Modernization: Use `str_contains()` in a few more places. `str_contains()` was introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) contains the given substring (needle). WordPress core includes a polyfill for `str_contains()` on PHP < 8.0 as of WordPress 5.9. This commit replaces `false !== strpos( ... )` with `str_contains()` in core files, making the code more readable and consistent, as well as better aligned with modern development practices. Follow-up to [55988]. Props spacedmonkey. See #58220. git-svn-id: https://develop.svn.wordpress.org/trunk@56021 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/blocks.php | 2 +- src/wp-includes/formatting.php | 5 +++-- src/wp-includes/link-template.php | 6 +++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index c747fe9426..3578b1d25e 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -803,7 +803,7 @@ function serialize_blocks( $blocks ) { function filter_block_content( $text, $allowed_html = 'post', $allowed_protocols = array() ) { $result = ''; - if ( false !== strpos( $text, '' ) ) { + if ( str_contains( $text, '' ) ) { $text = preg_replace_callback( '%%', '_filter_block_content_callback', $text ); } diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index dce33c19b2..19abf905b6 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -4463,8 +4463,9 @@ function esc_url( $url, $protocols = null, $_context = 'display' ) { * it needs http:// prepended (unless it's a relative link * starting with /, # or ?, or a PHP file). */ - if ( strpos( $url, ':' ) === false && ! in_array( $url[0], array( '/', '#', '?' ), true ) && - ! preg_match( '/^[a-z0-9-]+?\.php/i', $url ) ) { + if ( ! str_contains( $url, ':' ) && ! in_array( $url[0], array( '/', '#', '?' ), true ) && + ! preg_match( '/^[a-z0-9-]+?\.php/i', $url ) + ) { $url = 'http://' . $url; } diff --git a/src/wp-includes/link-template.php b/src/wp-includes/link-template.php index 2fe4483a7a..b59a30ad22 100644 --- a/src/wp-includes/link-template.php +++ b/src/wp-includes/link-template.php @@ -222,7 +222,7 @@ function get_permalink( $post = 0, $leavename = false ) { ) { $category = ''; - if ( strpos( $permalink, '%category%' ) !== false ) { + if ( str_contains( $permalink, '%category%' ) ) { $cats = get_the_category( $post->ID ); if ( $cats ) { $cats = wp_list_sort( @@ -260,7 +260,7 @@ function get_permalink( $post = 0, $leavename = false ) { } $author = ''; - if ( strpos( $permalink, '%author%' ) !== false ) { + if ( str_contains( $permalink, '%author%' ) ) { $authordata = get_userdata( $post->post_author ); $author = $authordata->user_nicename; } @@ -502,7 +502,7 @@ function get_attachment_link( $post = null, $leavename = false ) { $name = $post->post_name; } - if ( strpos( $parentlink, '?' ) === false ) { + if ( ! str_contains( $parentlink, '?' ) ) { $link = user_trailingslashit( trailingslashit( $parentlink ) . '%postname%' ); }