Fix fatal in is_oversize_media when usage data is a string#1176
Open
PatelUtkarsh wants to merge 3 commits into
Open
Fix fatal in is_oversize_media when usage data is a string#1176PatelUtkarsh wants to merge 3 commits into
is_oversize_media when usage data is a string#1176PatelUtkarsh wants to merge 3 commits into
Conversation
When Cloudinary is not yet connected (or the usage API returns an unexpected non-array response), Connect::$usage['media_limits'] can be a string. PHP 8.x then fatals with 'Cannot access offset of type string on string' as soon as any front-end request resolves an attachment URL (e.g. Elementor's get_the_post_thumbnail_url during wp_head). Guard the lookup and treat the asset as not oversize when limits are unavailable, matching the defensive pattern already used in Connect::get_usage_stat().
is_oversize_media when usage data is a string
When the Cloudinary usage API has never returned a valid response (fresh install, expired credentials, or a transient API error), both `Connect::$usage` and the persisted `last_usage` setting can be a string rather than the expected array. PHP 8.x then fatals with 'Cannot access offset of type string on string' in any code that dereferences these without checking. This adds defensive checks alongside the existing Media::is_oversize_media fix: - Plan_Details::plan() (php/ui/component/class-plan-details.php): bail to empty plan/requests values when last_usage is not an array. - settings-sidebar.php Account status description: guard the plan name access. - Special_Offer::is_special_offer_available(): return false when last_usage isn't a usable array. - Connect::usage_stats(): only cache derived max image/video sizes when media_limits is actually an array, so a stringy API response doesn't poison the transient and last_usage option for the rest of the hour.
Following the usage/last_usage fix, audit found the same PHP 8.x 'Cannot access offset of type string on string' / 'foreach on string' hazard in other code paths that read from get_option / get_transient and immediately dereference or iterate the result: - Connect::history(): $history option may be a string; reset to array before doing $history[$plan][$date] lookups. Also defend the $plan resolution when $this->usage or $this->credentials is unexpected. - Sync_Queue::get_thread_queue(): thread option may be a string; reset to defaults before array_merge. - Cron::load_schedule(): cron schedule option may be a string; reset to array before foreach and guard inner item type. - WPML::wp_generate_attachment_metadata(): transient may be a scalar; cast to array before the $data[$id] check. These are defensive guards only - normal happy-path behaviour is unchanged.
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.
Error
Triggered on PHP 8.x when any front-end request resolves an attachment URL (e.g. Elementor's
get_the_post_thumbnail_url()duringwp_head) while the plugin is not yet connected to Cloudinary, or when the usage API returns a non-array response. In those casesConnect::$usage['media_limits']is a string, andis_oversize_media()dereferenced it without checking.Approach
$connect->usageonce and verifymedia_limitsis an array containing the requested key before dereferencing.Connect::get_usage_stat().$limitfor thesize_format()message and drop the duplicate$max_sizerecompute.QA notes
Cannot access offset of type string on stringinclass-media.php:451.sync_errorpost meta is set with the expected message).