Skip to content

fix(admin-bar): prevent strpos() null deprecation on invalid API key#1026

Open
faisalahammad wants to merge 1 commit intowp-media:developfrom
faisalahammad:fix/issue-961-strpos-null-plan-label
Open

fix(admin-bar): prevent strpos() null deprecation on invalid API key#1026
faisalahammad wants to merge 1 commit intowp-media:developfrom
faisalahammad:fix/issue-961-strpos-null-plan-label

Conversation

@faisalahammad
Copy link
Copy Markdown

@faisalahammad faisalahammad commented Feb 27, 2026

Summary

Fixes PHP 8.1+ strpos() deprecation warning when hovering the Imagify admin bar menu with an invalid API key. The fix casts plan_label to string before passing it to strpos().

Fixes #961

Problem

When the API key is invalid, User::init_user() gets a WP_Error from get_imagify_user() and returns early without calling set_user_properties(). This leaves plan_label as null. When the admin bar profile AJAX fires, null is passed to strpos() in admin-bar-status.php, triggering:

PHP Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated
in views/admin/admin-bar-status.php on line 4

Solution

Cast $data['plan_label'] to (string) at the point of use in the view template. This is the minimal defensive fix — null becomes '', strpos('', '_') returns false, and the label displays as empty. No behavioral change for valid API keys.

Changes

views/admin/admin-bar-status.php

Before:

$pos        = strpos( $data['plan_label'], '_' );
$plan_label = false !== $pos ? substr( $data['plan_label'], 0, $pos ) : $data['plan_label'];

After:

$plan_label_value = (string) $data['plan_label'];
$pos              = strpos( $plan_label_value, '_' );
$plan_label       = false !== $pos ? substr( $plan_label_value, 0, $pos ) : $plan_label_value;

Why this works:
(string) cast converts null to '', satisfying PHP 8.1+'s strict type requirement for strpos(). When plan_label is a valid string (e.g. "free", "growth_monthly"), behavior is unchanged.

Testing

Manual Testing

Invalid API key:

  1. Fresh install Imagify, enter an invalid API key, save
  2. Hover Imagify in admin bar
  3. Check debug.log → no strpos() deprecation

Valid API key:

  1. Enter a valid API key, save
  2. Hover Imagify admin bar → plan info displays correctly

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() and ReflectionProperty deprecations).

Screen recording

https://videos.faisalahammad.com/recordings/Ti6Hma9pi0Xdgi8xPut3

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
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a PHP 8.1+ deprecation warning in the Imagify admin bar status view by ensuring the value passed to strpos() is always a string, even when the API key is invalid and plan_label remains null.

Changes:

  • Casts $data['plan_label'] to a string before calling strpos()/substr() in the admin bar status template.
  • Preserves existing behavior for valid plan labels while eliminating the deprecation notice for invalid API keys.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@remyperona remyperona added this to the 2.2.8 milestone Apr 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PHP Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated when hover admin bar while api key is invalid

3 participants