Empty array warnings and notices on first-time stats save
-
Found a bug that kicks out several debug messages.
Acting as a spammer to test the honeypot I get these messages:
Warning: array_key_exists() expects parameter 2 to be array, null given in /path/to/wp-content/plugins/honeypot/includes/wpa_functions.php on line 52 Notice: Undefined index: total in /path/to/wp-content/plugins/honeypot/includes/wpa_functions.php on line 62 Notice: Trying to access array offset on value of type null in /path/to/wp-content/plugins/honeypot/includes/wpa_functions.php on line 62 Notice: Trying to access array offset on value of type null in /path/to/wp-content/plugins/honeypot/includes/wpa_functions.php on line 62 Notice: Undefined index: week in /path/to/wp-content/plugins/honeypot/includes/wpa_functions.php on line 62 Notice: Trying to access array offset on value of type null in /path/to/wp-content/plugins/honeypot/includes/wpa_functions.php on line 62 Notice: Undefined index: month in /path/to/wp-content/plugins/honeypot/includes/wpa_functions.php on line 62 Notice: Trying to access array offset on value of type null in /path/to/wp-content/plugins/honeypot/includes/wpa_functions.php on line 62 Notice: Undefined index: all_time in /path/to/wp-content/plugins/honeypot/includes/wpa_functions.php on line 78
It’s only on the first time saving stats since all the array keys and values don’t exist yet. They “resolve themselves” after the first stat gets saved, but I personally don’t like seeing ANY messages in my debug logs.
Made several changes to the wpa_save_stats() function in /includes/wpa_functions.php to fix these messages. Here’s the re-worked function that does not kick out any messages on first-time stats save:
function wpa_save_stats($wp_system, $data){ $currentStats = get_option('wpa_stats') ? json_decode(get_option('wpa_stats'), true) : array(); $timeArray = array('today','week','month'); if (!array_key_exists($wp_system,$currentStats)){ $currentStats[$wp_system]['today']['count'] = 0; $currentStats[$wp_system]['week']['count'] = 0; $currentStats[$wp_system]['month']['count'] = 0; $currentStats[$wp_system]['today']['date'] = date('Ymd'); $currentStats[$wp_system]['week']['date'] = date('Ymd'); $currentStats[$wp_system]['month']['date'] = date('Ymd'); } foreach ($timeArray as $key => $time) { if ( isset($currentStats['total'][$time]['date']) && wpa_check_date($currentStats['total'][$time]['date'],$time)){ $currentStats['total'][$time]['count'] += 1; } else { $currentStats['total'][$time]['count'] = 1; } if ( isset($currentStats[$wp_system][$time]['date']) && wpa_check_date($currentStats[$wp_system][$time]['date'],$time)){ $currentStats[$wp_system][$time]['count'] += 1; } else { $currentStats[$wp_system][$time]['count'] = 1; } $currentStats['total'][$time]['date'] = date('Ymd'); $currentStats[$wp_system][$time]['date'] = date('Ymd'); } $currentStats['total']['all_time'] = isset($currentStats['total']['all_time']) ? $currentStats['total']['all_time'] + 1 : 1; @$currentStats[$wp_system]['all_time'] = isset($currentStats[$wp_system]['all_time']) ? $currentStats[$wp_system]['all_time'] + 1 : 1; update_option('wpa_stats', json_encode($currentStats)); }
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Empty array warnings and notices on first-time stats save’ is closed to new replies.