wp-spider-cache icon indicating copy to clipboard operation
wp-spider-cache copied to clipboard

Fix undefined array offset in get_keymaps() after offset slicing

Open Copilot opened this issue 3 months ago • 0 comments

The get_keymaps() function throws "Undefined offset: 1" notices on single-site installations when processing cache keys. After applying array_slice() to remove key prefixes, the code accesses $parts[1] without validation.

Changes

  • Add empty array check after offset slicing to skip malformed keys
  • Add isset($parts[1]) guard before accessing second element in multisite branch
// Remove key salts
if ( $offset > 0 ) {
    $parts = array_slice( $parts, $offset );
}

// Skip if not enough parts after offset
if ( empty( $parts ) ) {
    continue;
}

// Multisite means first part is numeric
if ( is_numeric( $parts[ 0 ] ) ) {
    // Skip if missing group part
    if ( ! isset( $parts[ 1 ] ) ) {
        continue;
    }
    $blog_id = (int) $parts[ 0 ];
    $group   = $parts[ 1 ];
    $global  = false;

This prevents notices while preserving existing behavior for well-formed cache keys.

Original prompt

This section details on the original issue you should resolve

<issue_title>Strange keys, undefined index</issue_title> <issue_description>Trying the plugin out, keep getting some odd behavior. The plugin was installed via composer (5.0.0), copied drop-ins, activated the plugin. At first, it didn't show any results on the admin page, although a number of SQL queries dropped, indicating the object cache is working. Installed php-memcache extension (by default, Laravel's Homestead box includes only php-memcached), now there are some keys, but they don't look real:

screen shot 2017-08-11 at 12 08 25

The value of all keys is the same and equals -1.

Also, I'm getting this notice dumped multiple times to debug.log:

[11-Aug-2017 09:01:59 UTC] PHP Notice:  Undefined offset: 1 in /home/vagrant/Code/ihorvorotnov/app/plugins/wp-spider-cache/wp-spider-cache.php on line 787
[11-Aug-2017 09:01:59 UTC] PHP Stack trace:
[11-Aug-2017 09:01:59 UTC] PHP   1. {main}() /home/vagrant/Code/ihorvorotnov/core/wp-admin/admin-ajax.php:0
[11-Aug-2017 09:01:59 UTC] PHP   2. do_action() /home/vagrant/Code/ihorvorotnov/core/wp-admin/admin-ajax.php:91
[11-Aug-2017 09:01:59 UTC] PHP   3. WP_Hook->do_action() /home/vagrant/Code/ihorvorotnov/core/wp-includes/plugin.php:453
[11-Aug-2017 09:01:59 UTC] PHP   4. WP_Hook->apply_filters() /home/vagrant/Code/ihorvorotnov/core/wp-includes/class-wp-hook.php:323
[11-Aug-2017 09:01:59 UTC] PHP   5. WP_Spider_Cache_UI->ajax_get_instance() /home/vagrant/Code/ihorvorotnov/core/wp-includes/class-wp-hook.php:298
[11-Aug-2017 09:01:59 UTC] PHP   6. WP_Spider_Cache_UI->do_rows() /home/vagrant/Code/ihorvorotnov/app/plugins/wp-spider-cache/wp-spider-cache.php:370
[11-Aug-2017 09:01:59 UTC] PHP   7. WP_Spider_Cache_UI->get_keymaps() /home/vagrant/Code/ihorvorotnov/app/plugins/wp-spider-cache/wp-spider-cache.php:1047

Line 787 is a multisite related check. The site is not multisite though.

My environment:

  • latest Laravel's Homestead,
  • PHP 7.1.8 (fpm, with Nginx),
  • php-memcache 3.0.9,
  • php-memcached 3.0.3,
  • Memcached 1.4.25,
  • Latest WP (4.8.1)

Some other plugins are installed - mostly dev related (Query Monitor, Laps etc). Deactivating them doesn't fix the problem.</issue_description>

Comments on the Issue (you are @copilot in this section)

  • Fixes stuttter/wp-spider-cache#27

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot avatar Dec 09 '25 03:12 Copilot