fix(admin-bar): prevent strpos() null deprecation on invalid API key#1026
Open
faisalahammad wants to merge 1 commit intowp-media:developfrom
Open
fix(admin-bar): prevent strpos() null deprecation on invalid API key#1026faisalahammad wants to merge 1 commit intowp-media:developfrom
faisalahammad wants to merge 1 commit intowp-media:developfrom
Conversation
Cast $data['plan_label'] to string before passing to strpos() to prevent PHP 8.1+ deprecation when the API key is invalid and plan_label is null. Fixes wp-media#961
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes PHP 8.1+
strpos()deprecation warning when hovering the Imagify admin bar menu with an invalid API key. The fix castsplan_labelto string before passing it tostrpos().Fixes #961
Problem
When the API key is invalid,
User::init_user()gets aWP_Errorfromget_imagify_user()and returns early without callingset_user_properties(). This leavesplan_labelasnull. When the admin bar profile AJAX fires,nullis passed tostrpos()inadmin-bar-status.php, triggering:Solution
Cast
$data['plan_label']to(string)at the point of use in the view template. This is the minimal defensive fix —nullbecomes'',strpos('', '_')returnsfalse, and the label displays as empty. No behavioral change for valid API keys.Changes
views/admin/admin-bar-status.php
Before:
After:
Why this works:
(string)cast convertsnullto'', satisfying PHP 8.1+'s strict type requirement forstrpos(). Whenplan_labelis a valid string (e.g."free","growth_monthly"), behavior is unchanged.Testing
Manual Testing
Invalid API key:
debug.log→ nostrpos()deprecationValid API key:
Automated Tests
$ composer test-unit OK, but incomplete, skipped, or risky tests! Tests: 24, Assertions: 59, Risky: 5.All 24 tests pass. The 5 risky tests are pre-existing (unrelated
version_compare()andReflectionPropertydeprecations).Screen recording
https://videos.faisalahammad.com/recordings/Ti6Hma9pi0Xdgi8xPut3