<?php
/**
 * Strips legacy CSS hover hacks from button variations on WP 7.0+.
 *
 * On WP 7.0+, native :hover keys on variations are processed by the theme.json engine,
 * so the css property hacks become redundant. This filter removes them to avoid duplicate CSS.
 * On WP < 7.0, the css hacks remain as the only hover mechanism (native :hover keys are ignored).
 */

namespace Brandy\BlockSettings\VariationHoverCompat;

use Brandy\Traits\SingletonTrait;

class Caller {
	use SingletonTrait;

	protected function __construct() {
		if ( version_compare( get_bloginfo( 'version' ), '7.0', '>=' ) ) {
			add_filter( 'wp_theme_json_data_theme', array( $this, 'strip_variation_hover_css_hacks' ) );
			add_filter( 'wp_theme_json_data_user', array( $this, 'strip_variation_hover_css_hacks' ) );
		}
	}

	/**
	 * Remove the css property from outline and secondary button variations.
	 * The native :hover keys in theme.json handle hover styles on WP 7.0+.
	 */
	public function strip_variation_hover_css_hacks( $theme_json ) {
		$data = $theme_json->get_data();

		if ( ! isset( $data['styles']['blocks']['core/button']['variations'] ) ) {
			return $theme_json;
		}

		unset( $data['styles']['blocks']['core/button']['variations']['outline']['css'] );
		unset( $data['styles']['blocks']['core/button']['variations']['secondary']['css'] );

		return $theme_json->update_with( $data );
	}
}

Caller::get_instance();
