From c2603744aacb58ebd962c0f6d5d1cbab787cfbea Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sun, 25 Jun 2023 10:50:24 +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. Built from https://develop.svn.wordpress.org/trunk@56021 git-svn-id: http://core.svn.wordpress.org/trunk@55533 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/blocks.php | 2 +- wp-includes/formatting.php | 5 +++-- wp-includes/link-template.php | 6 +++--- wp-includes/version.php | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/wp-includes/blocks.php b/wp-includes/blocks.php index c747fe9426..3578b1d25e 100644 --- a/wp-includes/blocks.php +++ b/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/wp-includes/formatting.php b/wp-includes/formatting.php index dce33c19b2..19abf905b6 100644 --- a/wp-includes/formatting.php +++ b/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/wp-includes/link-template.php b/wp-includes/link-template.php index 2fe4483a7a..b59a30ad22 100644 --- a/wp-includes/link-template.php +++ b/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%' ); } diff --git a/wp-includes/version.php b/wp-includes/version.php index 0c7435b0f4..1ef0504fe7 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.3-alpha-56020'; +$wp_version = '6.3-alpha-56021'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.