diff --git a/header-footer-grid/Core/Components/Logo.php b/header-footer-grid/Core/Components/Logo.php index 4de071fe98..5ffacc928a 100644 --- a/header-footer-grid/Core/Components/Logo.php +++ b/header-footer-grid/Core/Components/Logo.php @@ -153,7 +153,7 @@ final public function filter_svg_logo_size( $sizes, $size, $image_src, $image_me * @return string */ public function update_logo_theme_mod( $value, $old_value ) { - $conditional_main = json_decode( Mods::get( self::COMPONENT_ID . '_' . self::LOGO, self::sanitize_logo_json( $old_value ) ), true ); + $conditional_main = self::decode_logo_json( Mods::get( self::COMPONENT_ID . '_' . self::LOGO, self::sanitize_logo_json( $old_value ) ) ); $conditional_main['light'] = $value; if ( $conditional_main['same'] ) { $conditional_main['dark'] = $value; @@ -212,7 +212,7 @@ public function load_scripts() { */ public function filter_logo_variants( $variants ) { $main_logo = get_theme_mod( 'custom_logo' ); - $conditional_main = json_decode( Mods::get( self::COMPONENT_ID . '_' . self::LOGO, self::sanitize_logo_json( $main_logo ) ), true ); + $conditional_main = self::decode_logo_json( Mods::get( self::COMPONENT_ID . '_' . self::LOGO, self::sanitize_logo_json( $main_logo ) ) ); $logo_main_light_id = $main_logo; $logo_main_dark_id = $logo_main_light_id; $logo_main_same = true; @@ -328,6 +328,21 @@ public static function sanitize_logo_json( $input ) { ); } + /** + * Decode the stored logo palette, which may be a JSON string or an already decoded array. + * + * @param mixed $value Stored logo palette value. + * + * @return array + */ + public static function decode_logo_json( $value ): array { + if ( is_string( $value ) ) { + $value = json_decode( $value, true ); + } + + return is_array( $value ) ? $value : array(); + } + /** * Called to register component controls. * diff --git a/header-footer-grid/templates/components/component-logo.php b/header-footer-grid/templates/components/component-logo.php index 61c9172a06..1eaac24577 100644 --- a/header-footer-grid/templates/components/component-logo.php +++ b/header-footer-grid/templates/components/component-logo.php @@ -30,7 +30,7 @@ $custom_logo = component_setting( Logo::CUSTOM_LOGO, $main_logo ); $active_logo = $_id === 'logo' ? $main_logo : $custom_logo; -$conditional_logo = json_decode( component_setting( Logo::LOGO, Logo::sanitize_logo_json( $active_logo ) ), true ); +$conditional_logo = Logo::decode_logo_json( component_setting( Logo::LOGO, Logo::sanitize_logo_json( $active_logo ) ) ); $custom_logo_id = isset( $conditional_logo['light'] ) ? $conditional_logo['light'] : $active_logo; $wrapper_tag = 'p'; diff --git a/tests/test-neve-logo-palette.php b/tests/test-neve-logo-palette.php new file mode 100644 index 0000000000..85afb077ce --- /dev/null +++ b/tests/test-neve-logo-palette.php @@ -0,0 +1,155 @@ +previous_state = array( + 'builder' => Abstract_Builder::$current_builder, + 'builder_component' => Abstract_Builder::$current_component, + 'current_component' => Abstract_Component::$current_component, + 'no_cache' => Mods::$no_cache, + ); + Mods::$no_cache = true; + + $this->light_id = self::factory()->attachment->create_object( 'logo-light.png', 0, array( 'post_mime_type' => 'image/png' ) ); + $this->dark_id = self::factory()->attachment->create_object( 'logo-dark.png', 0, array( 'post_mime_type' => 'image/png' ) ); + + set_theme_mod( + Logo::COMPONENT_ID . '_' . Logo::LOGO, + array( + 'light' => $this->light_id, + 'dark' => $this->dark_id, + 'same' => false, + ) + ); + } + + /** + * Restore the global state. + */ + public function tear_down(): void { + remove_theme_mod( Logo::COMPONENT_ID . '_' . Logo::LOGO ); + remove_theme_mod( 'custom_logo' ); + + Abstract_Builder::$current_builder = $this->previous_state['builder']; + Abstract_Builder::$current_component = $this->previous_state['builder_component']; + Abstract_Component::$current_component = $this->previous_state['current_component']; + Mods::$no_cache = $this->previous_state['no_cache']; + + parent::tear_down(); + } + + /** + * Get the registered header logo component. + * + * @return Logo + */ + private function logo_component(): Logo { + $component = Main::get_instance()->get_builder( 'header' )->get_component( Logo::COMPONENT_ID ); + + $this->assertInstanceOf( Logo::class, $component ); + + return $component; + } + + /** + * Logo variants are built from a palette stored as an array. + */ + public function test_logo_variants_read_array_palette(): void { + $variants = $this->logo_component()->filter_logo_variants( array() ); + + $this->assertFalse( $variants[ Logo::COMPONENT_ID ]['same'] ); + $this->assertStringEndsWith( 'logo-light.png', $variants[ Logo::COMPONENT_ID ]['light']['src'] ); + $this->assertStringEndsWith( 'logo-dark.png', $variants[ Logo::COMPONENT_ID ]['dark']['src'] ); + } + + /** + * Changing the site logo updates a palette stored as an array. + */ + public function test_custom_logo_update_reads_array_palette(): void { + $new_logo_id = self::factory()->attachment->create_object( 'logo-new.png', 0, array( 'post_mime_type' => 'image/png' ) ); + + $this->assertSame( $new_logo_id, $this->logo_component()->update_logo_theme_mod( $new_logo_id, $this->light_id ) ); + $this->assertSame( + array( + 'light' => $new_logo_id, + 'dark' => $this->dark_id, + 'same' => false, + ), + json_decode( get_theme_mod( Logo::COMPONENT_ID . '_' . Logo::LOGO ), true ) + ); + } + + /** + * The logo template renders the light logo from a palette stored as an array. + */ + public function test_logo_template_reads_array_palette(): void { + $this->logo_component(); + Abstract_Builder::$current_builder = 'header'; + Abstract_Builder::$current_component = null; + Abstract_Component::$current_component = Logo::COMPONENT_ID; + + ob_start(); + Main::get_instance()->load( 'components/component-logo' ); + $output = ob_get_clean(); + + $this->assertStringContainsString( 'logo-light.png', $output ); + } + + /** + * Stored palettes decode to an array whatever their shape. + */ + public function test_decode_logo_json_normalizes_stored_values(): void { + $palette = array( + 'light' => 12, + 'dark' => 34, + 'same' => false, + ); + + $this->assertSame( $palette, Logo::decode_logo_json( $palette ) ); + $this->assertSame( $palette, Logo::decode_logo_json( wp_json_encode( $palette ) ) ); + $this->assertSame( array(), Logo::decode_logo_json( 'not json' ) ); + $this->assertSame( array(), Logo::decode_logo_json( '5' ) ); + $this->assertSame( array(), Logo::decode_logo_json( false ) ); + } +}