Check if array index exists before using it

WC_Geolite_Integration::get_country_iso() sometimes returns null instead of an array, so this commit adds a check to account for that possibility before using the returned value. Doing this to prevent the following undefined index notice:

PHP Notice: Undefined index: country in wp-content/plugins/woocommerce/includes/class-wc-geolite-integration.php on line 60

Fixes #20594
This commit is contained in:
Rodrigo Primo 2018-06-22 10:08:09 -03:00
parent 590af1dd2d
commit df3306084e
1 changed files with 6 additions and 3 deletions

View File

@ -55,9 +55,12 @@ class WC_Geolite_Integration {
$iso_code = '';
try {
$reader = new MaxMind\Db\Reader( $this->database ); // phpcs:ignore PHPCompatibility.PHP.NewLanguageConstructs.t_ns_separatorFound
$data = $reader->get( $ip_address );
$iso_code = $data['country']['iso_code'];
$reader = new MaxMind\Db\Reader( $this->database ); // phpcs:ignore PHPCompatibility.PHP.NewLanguageConstructs.t_ns_separatorFound
$data = $reader->get( $ip_address );
if ( isset( $data['country']['iso_code'] ) ) {
$iso_code = $data['country']['iso_code'];
}
$reader->close();
} catch ( Exception $e ) {