Code Modernization: Replace usage of strpos() with str_contains().

`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 [52039], [52040], [52326], [55703], [55710], [55987].

Props Soean, spacedmonkey, costdev, dingo_d, azaozz, mikeschroder, flixos90, peterwilsoncc, SergeyBiryukov.
Fixes #58206.

git-svn-id: https://develop.svn.wordpress.org/trunk@55988 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2023-06-22 14:34:56 +00:00
parent fdbd29eaba
commit 9e9887d8b8
100 changed files with 252 additions and 252 deletions
+7 -7
View File
@@ -2774,7 +2774,7 @@ function sanitize_post_field( $field, $value, $post_id, $context = 'display' ) {
}
$prefixed = false;
if ( false !== strpos( $field, 'post_' ) ) {
if ( str_contains( $field, 'post_' ) ) {
$prefixed = true;
$field_no_prefix = str_replace( 'post_', '', $field );
}
@@ -3273,7 +3273,7 @@ function wp_match_mime_types( $wildcard_mime_types, $real_mime_types ) {
$patternses[][ $type ] = "^$regex$";
if ( false === strpos( $mime, '/' ) ) {
if ( ! str_contains( $mime, '/' ) ) {
$patternses[][ $type ] = "^$regex/";
$patternses[][ $type ] = $regex;
}
@@ -3330,7 +3330,7 @@ function wp_post_mime_type_where( $post_mime_types, $table_alias = '' ) {
$mime_pattern = "$mime_group/$mime_subgroup";
} else {
$mime_pattern = preg_replace( '/[^-*.a-zA-Z0-9]/', '', $mime_type );
if ( false === strpos( $mime_pattern, '*' ) ) {
if ( ! str_contains( $mime_pattern, '*' ) ) {
$mime_pattern .= '/*';
}
}
@@ -3341,7 +3341,7 @@ function wp_post_mime_type_where( $post_mime_types, $table_alias = '' ) {
return '';
}
if ( false !== strpos( $mime_pattern, '%' ) ) {
if ( str_contains( $mime_pattern, '%' ) ) {
$wheres[] = empty( $table_alias ) ? "post_mime_type LIKE '$mime_pattern'" : "$table_alias.post_mime_type LIKE '$mime_pattern'";
} else {
$wheres[] = empty( $table_alias ) ? "post_mime_type = '$mime_pattern'" : "$table_alias.post_mime_type = '$mime_pattern'";
@@ -6124,10 +6124,10 @@ function get_pages( $args = array() ) {
* @return bool True on success, false on failure.
*/
function is_local_attachment( $url ) {
if ( strpos( $url, home_url() ) === false ) {
if ( ! str_contains( $url, home_url() ) ) {
return false;
}
if ( strpos( $url, home_url( '/?attachment_id=' ) ) !== false ) {
if ( str_contains( $url, home_url( '/?attachment_id=' ) ) ) {
return true;
}
@@ -6510,7 +6510,7 @@ function wp_get_attachment_url( $attachment_id = 0 ) {
if ( str_starts_with( $file, $uploads['basedir'] ) ) {
// Replace file location with url location.
$url = str_replace( $uploads['basedir'], $uploads['baseurl'], $file );
} elseif ( false !== strpos( $file, 'wp-content/uploads' ) ) {
} elseif ( str_contains( $file, 'wp-content/uploads' ) ) {
// Get the directory name relative to the basedir (back compat for pre-2.7 uploads).
$url = trailingslashit( $uploads['baseurl'] . '/' . _wp_get_attachment_relative_path( $file ) ) . wp_basename( $file );
} else {