Skip to content

fix(object-cache): ensure wp_cache_delete returns boolean - #1028

Open
faisalahammad wants to merge 1 commit into
litespeedtech:devfrom
faisalahammad:fix/924-wp-cache-delete-return-bool
Open

fix(object-cache): ensure wp_cache_delete returns boolean#1028
faisalahammad wants to merge 1 commit into
litespeedtech:devfrom
faisalahammad:fix/924-wp-cache-delete-return-bool

Conversation

@faisalahammad

Copy link
Copy Markdown
Contributor

Summary

Guarantees that wp_cache_delete() and WP_Object_Cache::delete() strictly return a boolean (true or false) rather than null. This prevents PHP TypeError fatal exceptions in PHP 8.x when standard object cache functions are called.

Fixes #924

Changes

Object Cache (src/object.lib.php & src/object-cache-wp.cls.php)

Before:

function wp_cache_delete( $key, $group = '' ) {
	global $wp_object_cache;

	return $wp_object_cache->delete( $key, $group );
}

After:

function wp_cache_delete( $key, $group = '' ) {
	global $wp_object_cache;

	if ( ! is_object( $wp_object_cache ) ) {
		return false;
	}

	return (bool) $wp_object_cache->delete( $key, $group );
}

Why: Directly returning the object cache method call could return null if the instance was uninitialized or returned non-boolean values. Adding an is_object() check and explicit (bool) typecast guarantees a boolean return value.

WP Object Cache Class (src/object-cache-wp.cls.php)

Before:

if ( $this->_object_cache->is_non_persistent( $group ) ) {
	return false;
}

return $this->_object_cache->delete( $id );

After:

if ( $this->is_non_persistent( $group ) || ! is_object( $this->_object_cache ) ) {
	return $found;
}

return (bool) $this->_object_cache->delete( $id );

Why: Ensures deletion from runtime memory is returned for non-persistent groups or missing instances, and persistent store deletion output is explicitly cast to bool.

Testing

Test 1: Delete cache key

  1. Run wp_cache_delete( 'test_key', 'default' ).
  2. Confirm return type is boolean false (or true if item exists) and never null.

Test 2: Delete cache key with uninitialized object cache

  1. Set $wp_object_cache = null and invoke wp_cache_delete( 'test_key' ).
  2. Confirm return value is false without throwing PHP TypeError.

@hi-hai
hi-hai changed the base branch from master to dev August 7, 2026 13:03
@hi-hai
hi-hai changed the base branch from dev to master August 7, 2026 13:03
@hi-hai

hi-hai commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator

Please use dev branch as the base

@faisalahammad
faisalahammad force-pushed the fix/924-wp-cache-delete-return-bool branch from 88a3c40 to 2bfd528 Compare August 7, 2026 14:49
@faisalahammad
faisalahammad changed the base branch from master to dev August 7, 2026 15:02
@faisalahammad

Copy link
Copy Markdown
Contributor Author

Rebased branch onto dev and updated target base to dev branch.

@faisalahammad
faisalahammad force-pushed the fix/924-wp-cache-delete-return-bool branch from 2bfd528 to 77d4d31 Compare August 7, 2026 15:18
- Add is_object check for global object cache in wp_cache_delete
- Explicitly cast delete return values to boolean in WP_Object_Cache and wp_cache_delete
- Add local group tracking for global and non-persistent groups

Fixes litespeedtech#924
@faisalahammad
faisalahammad force-pushed the fix/924-wp-cache-delete-return-bool branch from 77d4d31 to efe1036 Compare August 7, 2026 15:24
@faisalahammad

Copy link
Copy Markdown
Contributor Author

Resolved conflicts. PR is now clean against dev branch.

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.

Type error: wp_cache_delete can return null

2 participants