Deprecation notice (PHP >= 8.1) in includes/settings.php when "admin_notices" action is triggered early in WP admin
zach-adams opened this issue · comments
Describe the bug
In the function maybe_notice
in settings.php the first if statement calls strpos
with the first parameter being the result of a call to get_current_screen()
, however since get_current_screen()
can return null and this call has no null coalescing operator it can lead to this deprecation notice on WordPress admin pages:
PHP Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in <abspath>/wp-content/plugins/distributor/includes/settings.php on line 128
Code in question:
distributor/includes/settings.php
Lines 122 to 128 in 14b56e6
This issue only occurs when certain plugins & themes (specifically GravityForms 2.8.1, for me in this case) hook into the admin_head
action and trigger the admin_notices
action earlier than usual because the $current_screen
global only gets constructed after the admin_head
action is triggered (and before admin_notices
is triggered).
A quick & dirty fix is to add a null coalescing fallback to empty string on that first parameter like below, however something more robust can be discussed in a PR:
if ( 0 === strpos( get_current_screen()->parent_base ?? '', 'distributor' ) ) {
I'm unsure how many other plugins & themes trigger this issue or if it's just GravityForms, regardless since null
is a documented return value for get_current_screen()
that if statement should be refactored to account for it. I can create a PR for that fix & follow-up if this sounds like a valid issue.
Steps to Reproduce
To trigger this issue without a specific plugin/theme:
- Install & activate Distributor (v2.0.2)
- Add the following to your theme's
functions.php
to simulate an early calladmin_notices
:
add_action('admin_head', function() {
do_action('admin_notices');
});
- Visit any WordPress admin dashboard page
- Check PHP error log and/or WP Debug log for deprecation notice
To trigger this issue with GravityForms:
- Install & activate latest GravityForms (v2.8.1) & Distributor (v2.0.2)
- Visit any GravityForms admin page (i.e.
/wp-admin/admin.php?page=gf_edit_forms
) - Check PHP error log and/or WP Debug log for deprecation notice
Screenshots, screen recording, code snippet
Issue demonstration with GravityForms:
distributor-issue-gravityforms.mp4
Environment information
- Device: MacBook Pro 16-inch 2021, M1 Pro
- OS: Sonoma 14.2.1
- Browser & version: Chrome Dev (Version 122.0.6226.2)
WordPress information
### wp-core ###
version: 6.4.2
site_language: en_US
user_language: en_US
timezone: +00:00
permalink: /%postname%/
https_status: true
multisite: false
user_registration: 0
blog_public: 0
default_comment_status: open
environment_type: local
user_count: 1
dotorg_communication: true
### wp-active-theme ###
name: Twenty Twenty-Four (twentytwentyfour)
version: 1.0
author: the WordPress team
author_website: https://wordpress.org
parent_theme: none
theme_features: core-block-patterns, post-thumbnails, responsive-embeds, editor-styles, html5, automatic-feed-links, widgets-block-editor, block-templates
theme_path: <abspath>/wp-content/themes/twentytwentyfour
auto_update: Disabled
### wp-plugins-active (4) ###
Advanced Custom Fields PRO: version: 6.2.4, author: WP Engine, Auto-updates disabled
Classic Editor: version: 1.6.3, author: WordPress Contributors, Auto-updates disabled
Distributor: version: 2.0.2, author: 10up Inc., Auto-updates disabled
Query Monitor: version: 3.15.0, author: John Blackbourn, Auto-updates disabled
### wp-plugins-inactive (1) ###
Gravity Forms: version: 2.8.1, author: Gravity Forms, Auto-updates disabled
### wp-media ###
image_editor: WP_Image_Editor_GD
imagick_module_version: Not available
imagemagick_version: Not available
imagick_version: Not available
file_uploads: 1
post_max_size: 512M
upload_max_filesize: 512M
max_effective_size: 512 MB
max_file_uploads: 20
gd_version: 2.3.3
gd_formats: GIF, JPEG, PNG, WebP, BMP, AVIF, XPM
ghostscript_version: not available
### wp-server ###
server_architecture: Darwin 23.2.0 arm64
httpd_software: nginx/1.25.3
php_version: 8.2.14 64bit
php_sapi: fpm-fcgi
max_input_variables: 1000
time_limit: 500
memory_limit: 1024M
max_input_time: 500
upload_max_filesize: 512M
php_post_max_size: 512M
curl_version: 8.5.0 (SecureTransport) OpenSSL/3.2.0
suhosin: false
imagick_availability: false
pretty_permalinks: true
current: 2024-01-09T22:32:20+00:00
utc-time: Tuesday, 09-Jan-24 22:32:20 UTC
server-time: 2024-01-09T22:32:20+00:00
### wp-database ###
extension: mysqli
server_version: 8.0.33
client_version: mysqlnd 8.2.14
max_allowed_packet: 67108864
max_connections: 151
### wp-constants ###
WP_HOME: undefined
WP_SITEURL: undefined
WP_CONTENT_DIR: <abspath>/wp-content
WP_PLUGIN_DIR: <abspath>/plugins
WP_MEMORY_LIMIT: 1024M
WP_MAX_MEMORY_LIMIT: 512M
WP_DEBUG: true
WP_DEBUG_DISPLAY: true
WP_DEBUG_LOG: true
SCRIPT_DEBUG: true
WP_CACHE: false
CONCATENATE_SCRIPTS: undefined
COMPRESS_SCRIPTS: undefined
COMPRESS_CSS: undefined
WP_ENVIRONMENT_TYPE: local
WP_DEVELOPMENT_MODE: theme
DB_CHARSET: utf8
DB_COLLATE: undefined
### wp-filesystem ###
wordpress: writable
wp-content: writable
uploads: writable
plugins: writable
themes: writable
### distributor ###
0: 2.0.2
1: Yes
2: <email>
3:
Override Author Byline: Yes
Media Handling: Featured image and attached images
4: N/A
5: N/A
Code of Conduct
- I agree to follow this project's Code of Conduct