<?php
/**
 * Brentwood "Newspaper Columns" — a one-click toggle on the Kadence Row Layout
 * (kadence/rowlayout). When ON, the row's single column of content flows into
 * 2 (or 3) balanced newspaper-style columns: you write/paste the text into ONE
 * column and CSS splits it (fills the left column, continues at the top of the
 * next), instead of manually cutting the copy into side-by-side columns.
 *
 * Built the same way as bw-row-style.php (Brentwood Shadow / Border Radius):
 *   - assets/bw-newspaper-editor.js declares the attributes on kadence/rowlayout,
 *     adds the sidebar toggle + a 2/3 column count, and mirrors the classes onto
 *     the editor wrapper so the canvas previews the split.
 *   - This file (render_block) adds the bw-newspaper-flow (+ bw-news-3) class
 *     onto the row's outer wrapper (.kb-row-layout-wrap) on the front end.
 *   - assets/bw-newspaper.css paints it in both editor + front end.
 *
 * IMPORTANT implementation note (why the earlier hand-rolled attempt did nothing):
 * Kadence renders the column's inner content wrapper (.kt-inside-inner-col) as a
 * FLEX container (display:flex; flex-direction:column). CSS `column-count` is
 * IGNORED on a flex container — so the multicol has no effect unless we also force
 * that wrapper back to `display:block`. The stylesheet does exactly that.
 *
 * Meant for a SINGLE-column row of text. Nested Row Layouts inside are left alone
 * (the stylesheet restores their inner wrappers).
 *
 * @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-newspaper', $uri . '/assets/bw-newspaper.css', array(), filemtime( $theme . '/assets/bw-newspaper.css' ) );
	}
);

/**
 * Load the shared stylesheet into the Gutenberg editor canvas so the row shows
 * the column split 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-newspaper' );
		}
	}
);

/**
 * 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-newspaper-editor.js';
		if ( ! file_exists( $file ) ) {
			return;
		}
		wp_enqueue_script(
			'bw-newspaper-editor',
			get_stylesheet_directory_uri() . '/assets/bw-newspaper-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-newspaper-flow (+ bw-news-3) class onto the row's outer
 * wrapper (.kb-row-layout-wrap) when the toggle is on. Only the first
 * .kb-row-layout-wrap is touched (the row's own wrapper), so nested rows keep
 * their own settings.
 *
 * @param string $html  Rendered block HTML.
 * @param array  $block Parsed block (blockName, attrs, …).
 * @return string
 */
function bw_newspaper_render( $html, $block ) {
	if ( ( $block['blockName'] ?? '' ) !== 'kadence/rowlayout' ) {
		return $html;
	}
	if ( empty( $block['attrs']['bwNewspaper'] ) ) {
		return $html;
	}

	$cols    = isset( $block['attrs']['bwNewspaperCols'] ) ? (int) $block['attrs']['bwNewspaperCols'] : 2;
	$classes = 'bw-newspaper-flow' . ( 3 === $cols ? ' bw-news-3' : '' );

	wp_enqueue_style( 'bw-newspaper' );

	$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_newspaper_render', 12, 2 );
