Skip to content

Commit

Permalink
Code Modernization: Replace usage of strpos() with str_contains() (#3…
Browse files Browse the repository at this point in the history
…4137)

Co-authored-by: Brad Jorsch <[email protected]>
Co-authored-by: anomiex <[email protected]>

Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/6944828534
  • Loading branch information
jeherve authored and matticbot committed Nov 21, 2023
1 parent 91ee6c0 commit fdc2f6c
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 47 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
This is an alpha version! The changes listed here are not final.

### Changed
- Code Modernization: Replace usage of strpos() with str_contains()
- Code Modernization: Replace usage of strpos() with str_starts_with().
- Code Modernization: Replace usage of substr() with str_starts_with() and str_ends_with().
- General: updated PHP requirement to PHP 7.0+
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "wordpress-plugin",
"license": "GPL-2.0-or-later",
"require": {
"automattic/jetpack-device-detection": "^2.0.0"
"automattic/jetpack-device-detection": "^2.0.1-alpha"
},
"require-dev": {
"yoast/phpunit-polyfills": "1.1.0",
Expand Down
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rest/class.wp-super-cache-rest-get-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function callback( $request ) {

if ( defined( 'WPLOCKDOWN' ) ) {
$config_file = file_get_contents( $wp_cache_config_file );
if ( false === strpos( $config_file, "defined( 'WPLOCKDOWN' )" ) ) {
if ( ! str_contains( $config_file, "defined( 'WPLOCKDOWN' )" ) ) {
wp_cache_replace_line( '^.*WPLOCKDOWN', "if ( ! defined( 'WPLOCKDOWN' ) ) define( 'WPLOCKDOWN', " . $this->get_is_lock_down_enabled() . " );", $wp_cache_config_file );
}
}
Expand Down
8 changes: 8 additions & 0 deletions vendor/automattic/jetpack-device-detection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.0.1-alpha] - unreleased

This is an alpha version! The changes listed here are not final.

### Changed
- Code Modernization: add note of non-usage of PHP8+ functions yet.

## [2.0.0] - 2023-11-20
### Changed
- Updated required PHP version to >= 7.0. [#34192]
Expand Down Expand Up @@ -173,6 +180,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Moving jetpack_is_mobile into a package

[2.0.1-alpha]: https://github.com/Automattic/jetpack-device-detection/compare/v2.0.0...v2.0.1-alpha
[2.0.0]: https://github.com/Automattic/jetpack-device-detection/compare/v1.5.1...v2.0.0
[1.5.1]: https://github.com/Automattic/jetpack-device-detection/compare/v1.5.0...v1.5.1
[1.5.0]: https://github.com/Automattic/jetpack-device-detection/compare/v1.4.27...v1.5.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
* Class Device_Detection
*
* Determine if the current User Agent matches the passed $kind.
*
* Note: str_contains() and other PHP8+ functions that have a polyfill in core are not used here,
* as wp-includes/compat.php may not be loaded yet.
*/
class Device_Detection {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

/**
* A class providing device properties detection.
*
* Note: str_contains() and other PHP8+ functions that have a polyfill in core are not used here,
* as wp-includes/compat.php may not be loaded yet.
*/
class User_Agent_Info {

Expand Down
6 changes: 3 additions & 3 deletions vendor/composer/installed.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"packages": [
{
"name": "automattic/jetpack-device-detection",
"version": "2.0.0",
"version_normalized": "2.0.0.0",
"version": "2.0.1-alpha.1700575066",
"version_normalized": "2.0.1.0-alpha1700575066",
"dist": {
"type": "path",
"url": "/tmp/jetpack-build/Automattic/jetpack-device-detection",
"reference": "fc170a6b2d36f7c75c3494e1cb69dc299cf3f31a"
"reference": "b796ef2b07cc72e0ad88ac871e8d22f2f2e5c559"
},
"require": {
"php": ">=7.0"
Expand Down
6 changes: 3 additions & 3 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
),
'versions' => array(
'automattic/jetpack-device-detection' => array(
'pretty_version' => '2.0.0',
'version' => '2.0.0.0',
'reference' => 'fc170a6b2d36f7c75c3494e1cb69dc299cf3f31a',
'pretty_version' => '2.0.1-alpha.1700575066',
'version' => '2.0.1.0-alpha1700575066',
'reference' => 'b796ef2b07cc72e0ad88ac871e8d22f2f2e5c559',
'type' => 'jetpack-library',
'install_path' => __DIR__ . '/../automattic/jetpack-device-detection',
'aliases' => array(),
Expand Down
20 changes: 10 additions & 10 deletions wp-cache-phase2.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ function wpsc_get_accept_header() {
$accept = isset( $_SERVER['HTTP_ACCEPT'] ) ? strtolower( filter_var( $_SERVER['HTTP_ACCEPT'] ) ) : '';

foreach ( $accept_headers as $header ) {
if ( strpos( $accept, $header ) !== false ) {
if ( str_contains( $accept, $header ) ) {
$accept = 'application/json';
}
}
Expand Down Expand Up @@ -678,7 +678,7 @@ function wp_cache_check_mobile( $cache_key ) {
}
}
$accept = isset( $_SERVER['HTTP_ACCEPT'] ) ? strtolower( $_SERVER['HTTP_ACCEPT'] ) : '';
if ( strpos( $accept, 'wap' ) !== false ) {
if ( str_contains( $accept, 'wap' ) ) {
return $cache_key . '-' . 'wap';
}

Expand Down Expand Up @@ -804,7 +804,7 @@ function get_current_url_supercache_dir( $post_id = 0 ) {
if ( $post_id != 0 ) {
$site_url = site_url();
$permalink = get_permalink( $post_id );
if ( false === strpos( $permalink, $site_url ) ) {
if ( ! str_contains( $permalink, $site_url ) ) {
/*
* Sometimes site_url doesn't return the siteurl. See https://wordpress.org/support/topic/wp-super-cache-not-refreshing-post-after-comments-made
*/
Expand Down Expand Up @@ -1280,14 +1280,14 @@ function wpsc_create_debug_log( $filename = '', $username = '' ) {
$line = str_replace( $path_to_site, "ABSPATH/", $line );
$debug_log[ $t ] = $line;
foreach( $checks as $check ) {
if ( $$check && false !== strpos( $line, " /$check/" ) ) {
if ( $$check && str_contains( $line, " /$check/" ) ) {
unset( $debug_log[ $t ] );
}
}
if ( $filter ) {
if ( false !== strpos( $line, $filter ) && $exclude_filter ) {
if ( str_contains( $line, $filter ) && $exclude_filter ) {
unset( $debug_log[ $t ] );
} elseif ( false === strpos( $line, $filter ) && ! $exclude_filter ) {
} elseif ( ! str_contains( $line, $filter ) && ! $exclude_filter ) {
unset( $debug_log[ $t ] );
}
}
Expand All @@ -1306,7 +1306,7 @@ function wpsc_create_debug_log( $filename = '', $username = '' ) {
}

function wpsc_delete_url_cache( $url ) {
if ( false !== strpos( $url, '?' ) ) {
if ( str_contains( $url, '?' ) ) {
wp_cache_debug( 'wpsc_delete_url_cache: URL contains the character "?". Not deleting URL: ' . $url );
return false;
}
Expand Down Expand Up @@ -2566,7 +2566,7 @@ function wp_cache_phase2_clean_cache( $file_prefix ) {
wp_cache_debug( "wp_cache_phase2_clean_cache: Cleaning cache in $blog_cache_dir" );
if ( $handle = @opendir( $blog_cache_dir ) ) {
while ( false !== ( $file = @readdir( $handle ) ) ) {
if ( strpos( $file, $file_prefix ) !== false ) {
if ( str_contains( $file, $file_prefix ) ) {
if ( strpos( $file, '.html' ) ) {
// delete old wpcache files immediately
wp_cache_debug( "wp_cache_phase2_clean_cache: Deleting obsolete wpcache cache+meta files: $file" );
Expand Down Expand Up @@ -3197,7 +3197,7 @@ function wp_cache_post_id_gc( $post_id, $all = 'all' ) {
}

$permalink = trailingslashit( str_replace( get_option( 'home' ), '', get_permalink( $post_id ) ) );
if ( false !== strpos( $permalink, '?' ) ) {
if ( str_contains( $permalink, '?' ) ) {
wp_cache_debug( 'wp_cache_post_id_gc: NOT CLEARING CACHE. Permalink has a "?". ' . $permalink );
return false;
}
Expand Down Expand Up @@ -3308,7 +3308,7 @@ function wp_cache_post_change( $post_id ) {
$supercache_files_deleted = false;
if ( $handle = @opendir( $blog_cache_dir ) ) {
while ( false !== ( $file = readdir( $handle ) ) ) {
if ( strpos( $file, $file_prefix ) !== false ) {
if ( str_contains( $file, $file_prefix ) ) {
if ( strpos( $file, '.html' ) ) {
// delete old wpcache files immediately
wp_cache_debug( "wp_cache_post_change: Deleting obsolete wpcache cache+meta files: $file" );
Expand Down
81 changes: 56 additions & 25 deletions wp-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ function wpsc_is_boost_installed() {
$plugins = array_keys( get_plugins() );

foreach ( $plugins as $plugin ) {
if ( false !== strpos( $plugin, 'jetpack-boost.php' ) ) {
if ( str_contains( $plugin, 'jetpack-boost.php' ) ) {
return true;
}
}
Expand Down Expand Up @@ -593,20 +593,38 @@ function wp_cache_manager_error_checks() {
}

if ( ! $is_nginx && function_exists( "is_main_site" ) && true == is_main_site() ) {
if ( ! isset( $htaccess_path ) ) {
$home_path = trailingslashit( get_home_path() );
} else {
$home_path = $htaccess_path;
}
$scrules = implode( "\n", extract_from_markers( $home_path.'.htaccess', 'WPSuperCache' ) );
if ( $cache_enabled && $wp_cache_mod_rewrite && !$wp_cache_mobile_enabled && strpos( $scrules, addcslashes( str_replace( ', ', '|', $wp_cache_mobile_browsers ), ' ' ) ) ) {
echo '<div class="notice notice-warning"><h4>' . __( 'Mobile rewrite rules detected', 'wp-super-cache' ) . "</h4>";
echo "<p>" . __( 'For best performance you should enable "Mobile device support" or delete the mobile rewrite rules in your .htaccess. Look for the 2 lines with the text "2.0\ MMP|240x320" and delete those.', 'wp-super-cache' ) . "</p><p>" . __( 'This will have no affect on ordinary users but mobile users will see uncached pages.', 'wp-super-cache' ) . "</p></div>";
} elseif ( $wp_cache_mod_rewrite && $cache_enabled && $wp_cache_mobile_enabled && $scrules != '' && (
( '' != $wp_cache_mobile_prefixes && false === strpos( $scrules, addcslashes( str_replace( ', ', '|', $wp_cache_mobile_prefixes ), ' ' ) ) ) ||
( '' != $wp_cache_mobile_browsers && false === strpos( $scrules, addcslashes( str_replace( ', ', '|', $wp_cache_mobile_browsers ), ' ' ) ) ) )
if ( ! isset( $htaccess_path ) ) {
$home_path = trailingslashit( get_home_path() );
} else {
$home_path = $htaccess_path;
}

$scrules = implode( "\n", extract_from_markers( $home_path . '.htaccess', 'WPSuperCache' ) );
if (
$cache_enabled
&& $wp_cache_mod_rewrite
&& ! $wp_cache_mobile_enabled
&& strpos( $scrules, addcslashes( str_replace( ', ', '|', $wp_cache_mobile_browsers ), ' ' ) )
) {
?>
echo '<div class="notice notice-warning"><h4>' . esc_html__( 'Mobile rewrite rules detected', 'wp-super-cache' ) . '</h4>';
echo '<p>' . esc_html__( 'For best performance you should enable "Mobile device support" or delete the mobile rewrite rules in your .htaccess. Look for the 2 lines with the text "2.0\ MMP|240x320" and delete those.', 'wp-super-cache' ) . '</p><p>' . esc_html__( 'This will have no affect on ordinary users but mobile users will see uncached pages.', 'wp-super-cache' ) . '</p></div>';
} elseif (
$wp_cache_mod_rewrite
&& $cache_enabled
&& $wp_cache_mobile_enabled
&& $scrules != '' // phpcs:ignore Universal.Operators.StrictComparisons.LooseNotEqual
&& (
(
'' != $wp_cache_mobile_prefixes // phpcs:ignore Universal.Operators.StrictComparisons.LooseNotEqual
&& ! str_contains( $scrules, addcslashes( str_replace( ', ', '|', $wp_cache_mobile_prefixes ), ' ' ) )
)
|| (
'' != $wp_cache_mobile_browsers // phpcs:ignore Universal.Operators.StrictComparisons.LooseNotEqual
&& ! str_contains( $scrules, addcslashes( str_replace( ', ', '|', $wp_cache_mobile_browsers ), ' ' ) )
)
)
) {
?>
<div class="notice notice-warning"><h4><?php _e( 'Rewrite rules must be updated', 'wp-super-cache' ); ?></h4>
<p><?php _e( 'The rewrite rules required by this plugin have changed or are missing. ', 'wp-super-cache' ); ?>
<?php _e( 'Mobile support requires extra rules in your .htaccess file, or you can set the plugin to simple mode. Here are your options (in order of difficulty):', 'wp-super-cache' ); ?>
Expand All @@ -615,13 +633,20 @@ function wp_cache_manager_error_checks() {
<li> <?php printf( __( 'Delete the plugin mod_rewrite rules in %s.htaccess enclosed by <code># BEGIN WPSuperCache</code> and <code># END WPSuperCache</code> and let the plugin regenerate them by reloading this page.', 'wp-super-cache' ), $home_path ); ?></li>
<li> <?php printf( __( 'Add the rules yourself. Edit %s.htaccess and find the block of code enclosed by the lines <code># BEGIN WPSuperCache</code> and <code># END WPSuperCache</code>. There are two sections that look very similar. Just below the line <code>%%{HTTP:Cookie} !^.*(comment_author_|%s|wp-postpass_).*$</code> add these lines: (do it twice, once for each section)', 'wp-super-cache' ), $home_path, wpsc_get_logged_in_cookie() ); ?></p>
<div style='padding: 2px; margin: 2px; border: 1px solid #333; width:400px; overflow: scroll'><pre><?php echo "RewriteCond %{HTTP_user_agent} !^.*(" . addcslashes( str_replace( ', ', '|', $wp_cache_mobile_browsers ), ' ' ) . ").*\nRewriteCond %{HTTP_user_agent} !^(" . addcslashes( str_replace( ', ', '|', $wp_cache_mobile_prefixes ), ' ' ) . ").*"; ?></pre></div></li></ol></div><?php
}
}

if ( $cache_enabled && $super_cache_enabled && $wp_cache_mod_rewrite && $scrules == '' ) {
?><div class='notice notice-warning'><h4><?php _e( 'Rewrite rules must be updated', 'wp-super-cache' ); ?></h4>
<p><?php _e( 'The rewrite rules required by this plugin have changed or are missing. ', 'wp-super-cache' ); ?>
<?php _e( 'Scroll down the Advanced Settings page and click the <strong>Update Mod_Rewrite Rules</strong> button.', 'wp-super-cache' ); ?></p></div><?php
}
if (
$cache_enabled
&& $super_cache_enabled
&& $wp_cache_mod_rewrite
&& $scrules == '' // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
) {
?>
<div class='notice notice-warning'><h4><?php esc_html_e( 'Rewrite rules must be updated', 'wp-super-cache' ); ?></h4>
<p><?php esc_html_e( 'The rewrite rules required by this plugin have changed or are missing. ', 'wp-super-cache' ); ?>
<?php esc_html_e( 'Scroll down the Advanced Settings page and click the <strong>Update Mod_Rewrite Rules</strong> button.', 'wp-super-cache' ); ?></p></div>
<?php
}
}

if ( ! $is_nginx && $wp_cache_mod_rewrite && $super_cache_enabled && function_exists( 'apache_get_modules' ) ) {
Expand Down Expand Up @@ -2158,7 +2183,7 @@ function wp_cache_verify_config_file() {

if ( file_exists($wp_cache_config_file) ) {
$lines = implode( ' ', file( $wp_cache_config_file ) );
if( strpos( $lines, 'WPCACHEHOME' ) === false ) {
if ( ! str_contains( $lines, 'WPCACHEHOME' ) ) {
if( is_writeable_ACLSafe( $wp_cache_config_file ) ) {
@unlink( $wp_cache_config_file );
} else {
Expand Down Expand Up @@ -2460,7 +2485,10 @@ function wp_cache_files() {
}
}
while( false !== ( $file = readdir( $handle ) ) ) {
if ( strpos( $file, $file_prefix ) !== false && substr( $file, -4 ) == '.php' ) {
if ( // phpcs:ignore Generic.WhiteSpace.ScopeIndent.IncorrectExact
str_contains( $file, $file_prefix )
&& substr( $file, -4 ) == '.php' // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
) { // phpcs:ignore Generic.WhiteSpace.ScopeIndent.Incorrect
if ( false == file_exists( $blog_cache_dir . 'meta/' . $file ) ) {
@unlink( $blog_cache_dir . $file );
continue; // meta does not exist
Expand Down Expand Up @@ -2743,7 +2771,7 @@ function wp_cache_clean_legacy_files( $dir, $file_prefix ) {
continue;
}

if ( strpos( $file, $file_prefix ) !== false ) {
if ( str_contains( $file, $file_prefix ) ) {
if ( strpos( $file, '.html' ) ) {
// delete old WPCache files immediately
@unlink( $dir . $file);
Expand Down Expand Up @@ -2786,7 +2814,7 @@ function wp_cache_clean_expired($file_prefix) {
$now = time();
if ( $handle = @opendir( $blog_cache_dir ) ) {
while ( false !== ( $file = readdir( $handle ) ) ) {
if ( strpos( $file, $file_prefix ) !== false ) {
if ( str_contains( $file, $file_prefix ) ) {
if ( strpos( $file, '.html' ) ) {
@unlink( $blog_cache_dir . $file);
@unlink( $blog_cache_dir . 'meta/' . str_replace( '.html', '.meta', $file ) );
Expand Down Expand Up @@ -2915,7 +2943,10 @@ function wp_cache_check_site() {
}

// Check for broken front page
if ( isset( $wp_super_cache_front_page_text ) && $wp_super_cache_front_page_text != '' && false === strpos( $front_page[ 'body' ], $wp_super_cache_front_page_text ) ) {
if (
! empty( $wp_super_cache_front_page_text )
&& ! str_contains( $front_page['body'], $wp_super_cache_front_page_text )
) {
if ( !isset( $wp_super_cache_front_page_clear ) || ( isset( $wp_super_cache_front_page_clear ) && $wp_super_cache_front_page_clear == 0 ) ) {
wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] Front page is not correct! Please clear cache!', 'wp-super-cache' ), home_url() ), sprintf( __( 'Please visit %1$s to clear the cache as the front page of your site is not correct and missing the text, "%2$s"!', 'wp-super-cache' ), admin_url( 'options-general.php?page=wpsupercache' ), $wp_super_cache_front_page_text ) );
} else {
Expand Down

0 comments on commit fdc2f6c

Please sign in to comment.