Prevent fatal error if NULL is provided in array_search under Jetpack Stats (#50696)

* Avoid potential fatal error if $fields is not an array

* Changelog

* Prevent fatals when $data['general'] is not an object.

* PHPCS
This commit is contained in:
Miguel Pérez Pellicer 2024-08-16 13:53:01 +02:00 committed by GitHub
parent 9fa6c20a24
commit bb532b7a6f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 1 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Prevent fatal error if NULL is provided in array_search under Jetpack Stats

View File

@ -527,8 +527,13 @@ class Controller extends GenericController {
*/
public function format_data_value( $data, $stat, $report, $chart, $query_args ) {
if ( 'jetpack/stats' === $report ) {
$index = false;
// Get the index of the field to tally.
$index = array_search( $chart, $data['general']->visits->fields, true );
if ( isset( $data['general']->visits->fields ) && is_array( $data['general']->visits->fields ) ) {
$index = array_search( $chart, $data['general']->visits->fields, true );
}
if ( ! $index ) {
return null;
}