#65408 closed enhancement (fixed)
Code Modernization: Use str_contains() instead of strpos()
| Reported by: | Soean | Owned by: | audrasjb |
|---|---|---|---|
| Priority: | normal | Milestone: | 7.1 |
| Component: | General | Version: | |
| Severity: | normal | Keywords: | has-patch commit |
| Cc: | Focuses: | coding-standards |
Description
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 change replaces new introduced false !== strpos( ... ) and false === strpos( ... ) with str_contains() in core files, making the code more readable and consistent, as well as better aligned with modern development practices.
Change History (5)
This ticket was mentioned in PR #12085 on WordPress/wordpress-develop by @Soean.
5 months ago
#1
This ticket was mentioned in PR #12123 on WordPress/wordpress-develop by Dervish12.
4 months ago
#5
## Summary
Replace strpos() !== false with str_contains() in the Cover block's video provider detection logic (src/wp-includes/blocks/cover.php).
str_contains() was introduced in PHP 8.0. WordPress core includes a polyfill for str_contains() on PHP < 8.0 as of WordPress 5.9.
This PR replaces strpos( $lower_src, '...' ) !== false patterns with str_contains() in the Cover block, making the code more readable and consistent, as well as better aligned with modern PHP development practices.
Follow-up to r60269 and r60939.
## Changes
- Replaced 4 instances of
strpos( $lower_src, '...' ) !== falsewithstr_contains( $lower_src, '...' )in the video provider detection withinrender_block_core_cover().
## Test plan
- [ ]
npm run test:php -- --filter='cover'— all 47 tests pass - [ ]
./vendor/bin/phpcs --standard=phpcs.xml.dist src/wp-includes/blocks/cover.php— no PHPCS violations - [ ] Verify Cover block with embedded YouTube, Vimeo, VideoPress, and WordPress.tv videos renders correctly
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
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 PR replaces
false !== strpos( ... )andfalse === strpos( ... )withstr_contains()in core files, making the code more readable and consistent, as well as better aligned with modern development practices.Introduced in r60269 and r60939
Trac ticket: https://core.trac.wordpress.org/ticket/65408