<?php
/**
 * Brentwood "Row Style" — two one-click toggles on the Kadence Row Layout
 * (kadence/rowlayout): "Brentwood Shadow" and "Brentwood Border Radius". They
 * replace the manual Box Shadow / Border Radius (+ Overflow Hidden) controls so
 * authors (and clients) aren't faced with values to fiddle — just on/off. The
 * two toggles are independent (shadow only, radius only, or both).
 *
 * Built the same way as the Section Red Border feature (bw-redborder.php):
 *   - assets/bw-row-style-editor.js declares the attributes on kadence/rowlayout,
 *     adds the sidebar panel, and mirrors the classes onto the editor wrapper.
 *   - This file (render_block) adds the bw-row-shadow / bw-row-radius classes
 *     onto the row's outer wrapper (.kb-row-layout-wrap) on the front end.
 *   - assets/bw-row-style.css paints both (one file works in editor + front end,
 *     because both contexts carry .kb-row-layout-wrap on the same element).
 *
 * Standard values: shadow 0 2px 4px rgba(0,0,0,.08); radius 8px + overflow hidden.
 *
 * @package Kadence-Child
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Register the shared stylesheet (used by both the front end and the editor).
 */
add_action(
	'init',
	function () {
		$theme = get_stylesheet_directory();
		$uri   = get_stylesheet_directory_uri();
		wp_register_style( 'bw-row-style', $uri . '/assets/bw-row-style.css', array(), filemtime( $theme . '/assets/bw-row-style.css' ) );
	}
);

/**
 * Load the shared stylesheet into the Gutenberg editor canvas so the row shows
 * its shadow/radius while editing. enqueue_block_assets reaches the iframed
 * canvas; gated on is_admin() so the front end only loads it when a row actually
 * uses the feature (via render_block, below).
 */
add_action(
	'enqueue_block_assets',
	function () {
		if ( is_admin() ) {
			wp_enqueue_style( 'bw-row-style' );
		}
	}
);

/**
 * Load the editor script early (priority 1) so its attribute filter is in place
 * before Kadence registers kadence/rowlayout.
 */
add_action(
	'enqueue_block_editor_assets',
	function () {
		$theme = get_stylesheet_directory();
		$file  = $theme . '/assets/bw-row-style-editor.js';
		if ( ! file_exists( $file ) ) {
			return;
		}
		wp_enqueue_script(
			'bw-row-style-editor',
			get_stylesheet_directory_uri() . '/assets/bw-row-style-editor.js',
			array( 'wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components', 'wp-compose', 'wp-hooks', 'wp-i18n' ),
			filemtime( $file ),
			true
		);
	},
	1
);

/**
 * Front end: add the bw-row-shadow / bw-row-radius classes onto the row's outer
 * wrapper (.kb-row-layout-wrap) when the toggles are on. This is the same
 * element Kadence applies its own shadow/radius to, so the result matches.
 *
 * @param string $html  Rendered block HTML.
 * @param array  $block Parsed block (blockName, attrs, …).
 * @return string
 */
function bw_row_style_render( $html, $block ) {
	if ( ( $block['blockName'] ?? '' ) !== 'kadence/rowlayout' ) {
		return $html;
	}
	$shadow = ! empty( $block['attrs']['bwShadow'] );
	$radius = ! empty( $block['attrs']['bwRadius'] );
	if ( ! $shadow && ! $radius ) {
		return $html;
	}

	$add = array();
	if ( $shadow ) {
		$add[] = 'bw-row-shadow';
	}
	if ( $radius ) {
		$add[] = 'bw-row-radius';
	}
	$classes = implode( ' ', $add );

	wp_enqueue_style( 'bw-row-style' );

	// Inject the classes into the outer wrapper's class attribute. Only the
	// first .kb-row-layout-wrap is touched (the row's own wrapper), so nested
	// rows keep their own settings. preg_replace_callback avoids
	// $-interpolation in the replacement.
	$count = 0;
	$out   = preg_replace_callback(
		'/<div\b[^>]*\bkb-row-layout-wrap\b[^>]*>/',
		function ( $m ) use ( $classes ) {
			$tag = $m[0];
			if ( preg_match( '/\bclass="/', $tag ) ) {
				return preg_replace( '/\bclass="/', 'class="' . $classes . ' ', $tag, 1 );
			}
			return preg_replace( '/<div\b/', '<div class="' . $classes . '"', $tag, 1 );
		},
		$html,
		1,
		$count
	);

	return $count ? $out : $html;
}
add_filter( 'render_block', 'bw_row_style_render', 12, 2 );
