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], [56021].

See #58206.
Built from https://develop.svn.wordpress.org/trunk@56031


git-svn-id: http://core.svn.wordpress.org/trunk@55543 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2023-06-26 10:17:25 +00:00
parent 69f9f902aa
commit ad16732d27
13 changed files with 41 additions and 35 deletions

View File

@ -3319,7 +3319,7 @@ function wp_ajax_send_attachment_to_editor() {
}
$url = empty( $attachment['url'] ) ? '' : $attachment['url'];
$rel = ( strpos( $url, 'attachment_id' ) || get_attachment_link( $id ) == $url );
$rel = ( str_contains( $url, 'attachment_id' ) || get_attachment_link( $id ) === $url );
remove_filter( 'media_send_to_editor', 'image_media_send_to_editor' );

View File

@ -831,7 +831,7 @@ function media_upload_form_handler() {
if ( ! empty( $attachment['url'] ) ) {
$rel = '';
if ( strpos( $attachment['url'], 'attachment_id' ) || get_attachment_link( $send_id ) == $attachment['url'] ) {
if ( str_contains( $attachment['url'], 'attachment_id' ) || get_attachment_link( $send_id ) === $attachment['url'] ) {
$rel = " rel='attachment wp-att-" . esc_attr( $send_id ) . "'";
}
@ -1359,7 +1359,7 @@ function image_media_send_to_editor( $html, $attachment_id, $attachment ) {
$align = ! empty( $attachment['align'] ) ? $attachment['align'] : 'none';
$size = ! empty( $attachment['image-size'] ) ? $attachment['image-size'] : 'medium';
$alt = ! empty( $attachment['image_alt'] ) ? $attachment['image_alt'] : '';
$rel = ( strpos( $url, 'attachment_id' ) || get_attachment_link( $attachment_id ) === $url );
$rel = ( str_contains( $url, 'attachment_id' ) || get_attachment_link( $attachment_id ) === $url );
return get_image_send_to_editor( $attachment_id, $attachment['post_excerpt'], $attachment['post_title'], $align, $url, $rel, $size, $alt );
}

View File

@ -571,7 +571,7 @@ final class _WP_Editors {
if ( ! empty( $editor_styles ) ) {
// Force urlencoding of commas.
foreach ( $editor_styles as $key => $url ) {
if ( strpos( $url, ',' ) !== false ) {
if ( str_contains( $url, ',' ) ) {
$editor_styles[ $key ] = str_replace( ',', '%2C', $url );
}
}

View File

@ -1060,13 +1060,18 @@ class WP_Rewrite {
* 2) post ID, 3) page name, 4) timestamp (year, month, day, hour, second and
* minute all present). Set these flags now as we need them for the endpoints.
*/
if ( strpos( $struct, '%postname%' ) !== false
|| strpos( $struct, '%post_id%' ) !== false
|| strpos( $struct, '%pagename%' ) !== false
|| ( strpos( $struct, '%year%' ) !== false && strpos( $struct, '%monthnum%' ) !== false && strpos( $struct, '%day%' ) !== false && strpos( $struct, '%hour%' ) !== false && strpos( $struct, '%minute%' ) !== false && strpos( $struct, '%second%' ) !== false )
) {
if ( str_contains( $struct, '%postname%' )
|| str_contains( $struct, '%post_id%' )
|| str_contains( $struct, '%pagename%' )
|| ( str_contains( $struct, '%year%' )
&& str_contains( $struct, '%monthnum%' )
&& str_contains( $struct, '%day%' )
&& str_contains( $struct, '%hour%' )
&& str_contains( $struct, '%minute%' )
&& str_contains( $struct, '%second%' ) )
) {
$post = true;
if ( strpos( $struct, '%pagename%' ) !== false ) {
if ( str_contains( $struct, '%pagename%' ) ) {
$page = true;
}
}
@ -1074,7 +1079,7 @@ class WP_Rewrite {
if ( ! $post ) {
// For custom post types, we need to add on endpoints as well.
foreach ( get_post_types( array( '_builtin' => false ) ) as $ptype ) {
if ( strpos( $struct, "%$ptype%" ) !== false ) {
if ( str_contains( $struct, "%$ptype%" ) ) {
$post = true;
// This is for page style attachment URLs.
@ -1555,7 +1560,7 @@ class WP_Rewrite {
// Apache 1.3 does not support the reluctant (non-greedy) modifier.
$match = str_replace( '.+?', '.+', $match );
if ( strpos( $query, $this->index ) !== false ) {
if ( str_contains( $query, $this->index ) ) {
$rules .= 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA,L]\n";
} else {
$rules .= 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA,L]\n";

View File

@ -6990,7 +6990,7 @@ class wp_xmlrpc_server extends IXR_Server {
$preg_target = preg_quote( $pagelinkedto, '|' );
foreach ( $p as $para ) {
if ( strpos( $para, $pagelinkedto ) !== false ) { // It exists, but is it a link?
if ( str_contains( $para, $pagelinkedto ) ) { // It exists, but is it a link?
preg_match( '|<a[^>]+?' . $preg_target . '[^>]*>([^>]+?)</a>|', $para, $context );
// If the URL isn't in a link context, keep looking.

View File

@ -1019,7 +1019,7 @@ function wp_specialchars_decode( $text, $quote_style = ENT_NOQUOTES ) {
}
// Don't bother if there are no entities - saves a lot of processing.
if ( strpos( $text, '&' ) === false ) {
if ( ! str_contains( $text, '&' ) ) {
return $text;
}
@ -2474,7 +2474,7 @@ function convert_chars( $content, $deprecated = '' ) {
_deprecated_argument( __FUNCTION__, '0.71' );
}
if ( strpos( $content, '&' ) !== false ) {
if ( str_contains( $content, '&' ) ) {
$content = preg_replace( '/&([^#])(?![a-z1-4]{1,8};)/i', '&#038;$1', $content );
}
@ -2525,7 +2525,7 @@ function convert_invalid_entities( $content ) {
'&#159;' => '&#376;',
);
if ( strpos( $content, '&#1' ) !== false ) {
if ( str_contains( $content, '&#1' ) ) {
$content = strtr( $content, $wp_htmltranswinuni );
}
@ -5581,7 +5581,7 @@ function _sanitize_text_fields( $str, $keep_newlines = false ) {
$filtered = wp_check_invalid_utf8( $str );
if ( strpos( $filtered, '<' ) !== false ) {
if ( str_contains( $filtered, '<' ) ) {
$filtered = wp_pre_kses_less_than( $filtered );
// This will strip extra whitespace for us.
$filtered = wp_strip_all_tags( $filtered, false );
@ -6066,7 +6066,7 @@ function wp_staticize_emoji_for_email( $mail ) {
}
foreach ( $headers as $header ) {
if ( strpos( $header, ':' ) === false ) {
if ( ! str_contains( $header, ':' ) ) {
continue;
}
@ -6078,7 +6078,7 @@ function wp_staticize_emoji_for_email( $mail ) {
$content = trim( $content );
if ( 'content-type' === strtolower( $name ) ) {
if ( strpos( $content, ';' ) !== false ) {
if ( str_contains( $content, ';' ) ) {
list( $type, $charset ) = explode( ';', $content );
$content_type = trim( $type );
} else {

View File

@ -6104,7 +6104,7 @@ function wp_guess_url() {
$script_filename_dir = dirname( $_SERVER['SCRIPT_FILENAME'] );
// The request is for the admin.
if ( strpos( $_SERVER['REQUEST_URI'], 'wp-admin' ) !== false || str_contains( $_SERVER['REQUEST_URI'], 'wp-login.php' ) ) {
if ( str_contains( $_SERVER['REQUEST_URI'], 'wp-admin' ) || str_contains( $_SERVER['REQUEST_URI'], 'wp-login.php' ) ) {
$path = preg_replace( '#/(wp-admin/?.*|wp-login\.php.*)#i', '', $_SERVER['REQUEST_URI'] );
// The request is for a file in ABSPATH.

View File

@ -907,9 +907,11 @@ function get_bloginfo( $show = '', $filter = 'raw' ) {
}
$url = true;
if ( strpos( $show, 'url' ) === false &&
strpos( $show, 'directory' ) === false &&
strpos( $show, 'home' ) === false ) {
if ( ! str_contains( $show, 'url' )
&& ! str_contains( $show, 'directory' )
&& ! str_contains( $show, 'home' )
) {
$url = false;
}
@ -3738,7 +3740,7 @@ function user_can_richedit() {
if ( $is_safari ) {
$wp_rich_edit = ! wp_is_mobile() || ( preg_match( '!AppleWebKit/(\d+)!', $_SERVER['HTTP_USER_AGENT'], $match ) && (int) $match[1] >= 534 );
} elseif ( $is_IE ) {
$wp_rich_edit = ( strpos( $_SERVER['HTTP_USER_AGENT'], 'Trident/7.0;' ) !== false );
$wp_rich_edit = str_contains( $_SERVER['HTTP_USER_AGENT'], 'Trident/7.0;' );
} elseif ( $is_gecko || $is_chrome || $is_edge || ( $is_opera && ! wp_is_mobile() ) ) {
$wp_rich_edit = true;
}

View File

@ -73,14 +73,12 @@ function wp_fix_server_vars() {
}
// Fix for PHP as CGI hosts that set SCRIPT_FILENAME to something ending in php.cgi for all requests.
if ( isset( $_SERVER['SCRIPT_FILENAME'] )
&& ( strpos( $_SERVER['SCRIPT_FILENAME'], 'php.cgi' ) === strlen( $_SERVER['SCRIPT_FILENAME'] ) - 7 )
) {
if ( isset( $_SERVER['SCRIPT_FILENAME'] ) && str_ends_with( $_SERVER['SCRIPT_FILENAME'], 'php.cgi' ) ) {
$_SERVER['SCRIPT_FILENAME'] = $_SERVER['PATH_TRANSLATED'];
}
// Fix for Dreamhost and other PHP as CGI hosts.
if ( isset( $_SERVER['SCRIPT_NAME'] ) && ( strpos( $_SERVER['SCRIPT_NAME'], 'php.cgi' ) !== false ) ) {
if ( isset( $_SERVER['SCRIPT_NAME'] ) && str_contains( $_SERVER['SCRIPT_NAME'], 'php.cgi' ) ) {
unset( $_SERVER['PATH_INFO'] );
}

View File

@ -4059,9 +4059,10 @@ function wp_plupload_default_settings() {
* but iOS 7.x has a bug that prevents uploading of videos when enabled.
* See #29602.
*/
if ( wp_is_mobile() && strpos( $_SERVER['HTTP_USER_AGENT'], 'OS 7_' ) !== false &&
strpos( $_SERVER['HTTP_USER_AGENT'], 'like Mac OS X' ) !== false ) {
if ( wp_is_mobile()
&& str_contains( $_SERVER['HTTP_USER_AGENT'], 'OS 7_' )
&& str_contains( $_SERVER['HTTP_USER_AGENT'], 'like Mac OS X' )
) {
$defaults['multi_selection'] = false;
}

View File

@ -1538,7 +1538,7 @@ class WP_REST_Server {
$data['endpoints'][] = $endpoint_data;
// For non-variable routes, generate links.
if ( strpos( $route, '{' ) === false ) {
if ( ! str_contains( $route, '{' ) ) {
$data['_links'] = array(
'self' => array(
array(

View File

@ -1111,7 +1111,7 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
foreach ( $disposition_header as $value ) {
$value = trim( $value );
if ( strpos( $value, ';' ) === false ) {
if ( ! str_contains( $value, ';' ) ) {
continue;
}
@ -1121,7 +1121,7 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
$attributes = array();
foreach ( $attr_parts as $part ) {
if ( strpos( $part, '=' ) === false ) {
if ( ! str_contains( $part, '=' ) ) {
continue;
}

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.3-alpha-56030';
$wp_version = '6.3-alpha-56031';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.