google / site-kit-wp

Site Kit is a one-stop solution for WordPress users to use everything Google has to offer to make them successful on the web.
https://sitekit.withgoogle.com
Apache License 2.0
1.25k stars 291 forks source link

PHP 8.1 Deprecation notice: `strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated` #7147

Closed jamesozzie closed 1 year ago

jamesozzie commented 1 year ago

Bug Description

As reported by one user in the support forums, when using PHP 8.1 the following PHP errors appear:

Deprecated | strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated | +wp-includes/functions.php:7053 | 1 | Plugin: google-site-kit
Deprecated | str_replace(): Passing null to parameter #3 ($subject) of type array\|string is deprecated | +wp-includes/functions.php:2165

The error as it appears on the front end of a site

image

The error as it appears from wp-admin

image

Steps to reproduce

  1. Set up Site Kit on a new WordPress site with PHP 8.1
  2. Enable error reporting

Additional Context

The debug.log should be inspected to confirm the results here as not everything is visible via plugins like Query Monitor.

e.g.

// in wp-config.php
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );

The wp-content/debug.log file can be inspected easily using the Code Editor on instawp.


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

Implementation Brief

Test Coverage

QA Brief

// in wp-config.php
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );

Changelog entry

aaemnnosttv commented 1 year ago

This should be a very low effort since most of it is already done so the estimate should almost entirely be for QA.

tofumatt commented 1 year ago

IB ✅

tofumatt commented 1 year ago

Moving this right to code review as all that was needed was a QA Brief, so I wrote that up and reviewed the PR itself 🙂

mohitwp commented 1 year ago

QA Update ✅

https://github.com/google/site-kit-wp/assets/94359491/6e3757a1-ba83-44f6-b022-57cdef68cd48

mohitwp commented 1 year ago

QA Update ⚠️

Moving it back to QA from Approval. @tofumatt I found that in front end Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /home/fusupexasa9642/web/ly-unicorn-luke.instawp.xyz/public_html/wp-content/plugins/google-site-kit/includes/Core/REST_API/REST_Routes.php on line 69 notice is appearing on PHP 8.1.

In back end now PHP deprecated notices are not showing, but it's still showing in front end. Since @aaemnnosttv is not available this week. Can you please guide weather should I create separate ticket for this or you will resolve this in follow-up PR ?

image

tofumatt commented 1 year ago

@mohitwp Looks like that's a legit bug, thanks for catching that. It's related to this issue so I've created a follow-up PR.

Turns out I didn't 100% fix the issue as I didn't quite get the IB as written so thought Evan's original PR was good to merge 😓

techanvil commented 1 year ago

Back to you for another pass, @mohitwp.

mohitwp commented 1 year ago

QA Update ✅

Thanks @tofumatt @techanvil !

https://github.com/google/site-kit-wp/assets/94359491/23c59066-eda1-492d-9dbc-9795dc04228c https://github.com/google/site-kit-wp/assets/94359491/d10aa7d5-85d1-4458-9256-50bfc03b9267
sajidoon commented 1 year ago

Could you please replace your current function with the following one? It should solve the issue you're experiencing.

Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /homec/sajidhostriplex/public_html/whmtest/wp-includes/functions.php on line 2182

function wp_is_stream( $path ) { // Check if $path is null or empty if ( is_null($path) || empty($path) ) { // $path is null or empty, it can't be a stream. return false; }

$scheme_separator = strpos( $path, '://' );

if ( false === $scheme_separator ) {
    // $path isn't a stream.
    return false;
}

$stream = substr( $path, 0, $scheme_separator );

return in_array( $stream, stream_get_wrappers(), true );

}

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /homec/sajidhostriplex/public_html/whmtest/wp-includes/functions.php on line 7127

function wp_normalize_path( $path ) { $wrapper = '';

if ( wp_is_stream( $path ) ) {
    list( $wrapper, $path ) = explode( '://', $path, 2 );

    $wrapper .= '://';
}

// Check if $path is null or empty
if ( is_null($path) || empty($path) ) {
    return $wrapper; // Return the wrapper part as is if $path is null or empty
}

// Standardize all paths to use '/'.
$path = str_replace( '\\', '/', $path );

// Replace multiple slashes down to a singular, allowing for network shares having two slashes.
$path = preg_replace( '|(?<=.)/+|', '/', $path );

// Windows paths should uppercase the drive letter.
if ( ':' === substr( $path, 1, 1 ) ) {
    $path = ucfirst( $path );
}

return $wrapper . $path;

}

mxbclang commented 1 year ago

@sajidoon Thanks for getting in touch! This issue is now closed and a fix was released in Site Kit v1.108.0. If you're still experiencing issues, can you please open a topic on our support forum and we can take a look? Thanks!