PHP Warning: Undefined array key “enabled” after update to 4.0.13
-
PHP Warning: Undefined array key “enabled” after update to 4.0.13
After updating Payment Plugins for Stripe WooCommerce to version 4.0.13, the following PHP warning started appearing repeatedly:
PHP Warning: Undefined array key "enabled" in /wp-content/plugins/woo-stripe-payment/includes/class-wc-stripe-product-gateway-option.php on line 113The issue is in
WC_Stripe_Product_Gateway_Option::get_option():public function get_option( $key, $default = null ) { if ( ! isset( $this->settings[ $key ] ) && null != $default ) { $this->settings[ $key ] = $default; } return $this->settings[ $key ]; }enabled()calls:return $this->get_option( 'enabled', false );Because the comparison in
get_option()is non-strict (null != $default), it evaluates to false when$defaultis booleanfalse. Therefore, if theenabledkey is missing, the default value is not assigned and the subsequent array access triggers the PHP warning.I tested the following fix successfully:
public function get_option( $key, $default = null ) { if ( ! isset( $this->settings[ $key ] ) && null !== $default ) { $this->settings[ $key ] = $default; } return $this->settings[ $key ] ?? $default; }This changes the comparison to strict (
!==) and also safely returns the supplied default if the key is absent.Could you please include this fix in the next release?
You must be logged in to reply to this topic.