Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions header-footer-grid/Core/Components/Logo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<array-key, mixed>
*/
public static function decode_logo_json( $value ): array {
if ( is_string( $value ) ) {
$value = json_decode( $value, true );
}

return is_array( $value ) ? $value : array();
Comment on lines +338 to +343

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note decode_logo_json() now passes any stored array through without checking its members. If a stored palette array holds a non-scalar light or dark, that value reaches wp_get_attachment_image_url() in filter_logo_variants() and $custom_logo_id in the logo template. Can Neve Pro conditional headers or a starter-site import store such an array? If they only store attachment IDs, this is safe. If they can store nested values, the logo breaks or warns.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changed: a palette stored as a JSON string already decodes to the same nested shape through json_decode( ..., true ) on the base code, so member handling is untouched. This PR only stops array-stored palettes from crashing. The only writers (the Customizer palette control via sanitize_logo_json(), and the starter-site importer copying exported Customizer values) store attachment IDs for light/dark.

}

/**
* Called to register component controls.
*
Expand Down
2 changes: 1 addition & 1 deletion header-footer-grid/templates/components/component-logo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
155 changes: 155 additions & 0 deletions tests/test-neve-logo-palette.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php
/**
* Tests for the logo palette setting read paths.
*
* @package neve
*/

use HFG\Core\Builder\Abstract_Builder;
use HFG\Core\Components\Abstract_Component;
use HFG\Core\Components\Logo;
use HFG\Main;
use Neve\Core\Settings\Mods;

/**
* Class TestNeveLogoPalette
*/
class TestNeveLogoPalette extends WP_UnitTestCase {

/**
* Previous render state and Mods cache mode.
*
* @var array{builder: mixed, builder_component: mixed, current_component: mixed, no_cache: bool}
*/
private $previous_state;

/**
* Light logo attachment id.
*
* @var int
*/
private $light_id;

/**
* Dark logo attachment id.
*
* @var int
*/
private $dark_id;

/**
* Store the global state the tests change and store the palette as an array.
*/
public function set_up(): void {
parent::set_up();

$this->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 ) );
}
}
Loading