diff --git a/facebook-commerce-pixel-event.php b/facebook-commerce-pixel-event.php index 0e1ca5daf..2a78fd81b 100644 --- a/facebook-commerce-pixel-event.php +++ b/facebook-commerce-pixel-event.php @@ -657,15 +657,27 @@ private static function get_version_info() { * Get PixelID related settings. */ public static function get_options() { - $facebook_config = get_option( self::SETTINGS_KEY ); - return is_array( $facebook_config ) && ! empty( $facebook_config ) - ? $facebook_config - : array( - self::PIXEL_ID_KEY => '0', - self::USE_PII_KEY => 0, - self::USE_S2S_KEY => false, - self::ACCESS_TOKEN_KEY => '', - ); + + $default_options = array( + self::PIXEL_ID_KEY => '0', + self::USE_PII_KEY => 0, + self::USE_S2S_KEY => false, + self::ACCESS_TOKEN_KEY => '', + ); + + $fb_options = get_option( self::SETTINGS_KEY ); + + if ( ! is_array( $fb_options ) ) { + $fb_options = $default_options; + } else { + foreach ( $default_options as $key => $value ) { + if ( ! isset( $fb_options[ $key ] ) ) { + $fb_options[ $key ] = $value; + } + } + } + + return $fb_options; } /** diff --git a/tests/Unit/FacebookCommercePixelEventTest.php b/tests/Unit/FacebookCommercePixelEventTest.php new file mode 100644 index 000000000..23f74ee85 --- /dev/null +++ b/tests/Unit/FacebookCommercePixelEventTest.php @@ -0,0 +1,61 @@ + '0', + WC_Facebookcommerce_Pixel::USE_PII_KEY => 0, + WC_Facebookcommerce_Pixel::USE_S2S_KEY => false, + WC_Facebookcommerce_Pixel::ACCESS_TOKEN_KEY => '', + ); + + $actual_options = WC_Facebookcommerce_Pixel::get_options(); + + $this->assertEquals( $expected_options, $actual_options ); + } + + public function test_get_options_returns_merged_options_when_options_exist() { + $default_options = array( + WC_Facebookcommerce_Pixel::PIXEL_ID_KEY => '0', + WC_Facebookcommerce_Pixel::USE_PII_KEY => 0, + WC_Facebookcommerce_Pixel::USE_S2S_KEY => false, + WC_Facebookcommerce_Pixel::ACCESS_TOKEN_KEY => '', + ); + + $existing_options = array( + WC_Facebookcommerce_Pixel::PIXEL_ID_KEY => '123456789', + WC_Facebookcommerce_Pixel::USE_PII_KEY => 1, + WC_Facebookcommerce_Pixel::USE_S2S_KEY => true, + WC_Facebookcommerce_Pixel::ACCESS_TOKEN_KEY => 'abc123', + ); + + $expected_options = array_merge( $default_options, $existing_options ); + + update_option( WC_Facebookcommerce_Pixel::SETTINGS_KEY, $existing_options ); + + $actual_options = WC_Facebookcommerce_Pixel::get_options(); + + $this->assertEquals( $expected_options, $actual_options ); + } + + public function test_get_options_returns_default_options_when_options_are_not_an_array() { + update_option( WC_Facebookcommerce_Pixel::SETTINGS_KEY, 'not an array' ); + + $expected_options = array( + WC_Facebookcommerce_Pixel::PIXEL_ID_KEY => '0', + WC_Facebookcommerce_Pixel::USE_PII_KEY => 0, + WC_Facebookcommerce_Pixel::USE_S2S_KEY => false, + WC_Facebookcommerce_Pixel::ACCESS_TOKEN_KEY => '', + ); + + $actual_options = WC_Facebookcommerce_Pixel::get_options(); + + $this->assertEquals( $expected_options, $actual_options ); + } + +}