<?php
/**
 * Opt-in image lightbox for the Kadence Advanced Image and core Image blocks.
 *
 * Authors flip "Lightbox → Open in lightbox on click" on an image (the toggle is
 * declared in assets/bw-lightbox-editor.js). On the front end we tag that image's
 * <figure> with data attributes; the shared, dependency-free assets/bw-lightbox.js
 * makes it open full-size in an overlay. One overlay handles both block types, so
 * the experience is identical regardless of which image block was used.
 *
 * The front-end assets are tiny and enqueued unconditionally: bw-lightbox.js
 * no-ops on pages with no tagged image, and the CSS only styles markup the script
 * creates. (Enqueuing conditionally from render_block is unreliable for styles,
 * which would already have been printed in <head>.)
 *
 * @package Kadence-Child
 */

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

/* Editor: load the toggle filter early, before Kadence registers kadence/image. */
add_action(
	'enqueue_block_editor_assets',
	function () {
		$theme = get_stylesheet_directory();
		$file  = $theme . '/assets/bw-lightbox-editor.js';
		if ( ! file_exists( $file ) ) {
			return;
		}
		wp_enqueue_script(
			'bw-lightbox-editor',
			get_stylesheet_directory_uri() . '/assets/bw-lightbox-editor.js',
			array( 'wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components', 'wp-compose', 'wp-hooks', 'wp-i18n' ),
			filemtime( $file ),
			true
		);
	},
	1
);

/* Front end: load the lightbox CSS + JS. */
add_action(
	'wp_enqueue_scripts',
	function () {
		$theme = get_stylesheet_directory();
		$uri   = get_stylesheet_directory_uri();
		wp_enqueue_style( 'bw-lightbox', $uri . '/assets/bw-lightbox.css', array(), filemtime( $theme . '/assets/bw-lightbox.css' ) );
		wp_enqueue_script( 'bw-lightbox', $uri . '/assets/bw-lightbox.js', array(), filemtime( $theme . '/assets/bw-lightbox.js' ), true );
	}
);

/**
 * Tag a lightbox-enabled image's <figure> so bw-lightbox.js can wire it up.
 * Adds data-bw-lb plus the full-size image URL (resolved from the attachment ID,
 * falling back to the on-page <img> src).
 *
 * @param string $html  Rendered block HTML.
 * @param array  $block Parsed block.
 * @return string
 */
function bw_lightbox_render( $html, $block ) {
	$name = $block['blockName'] ?? '';
	if ( 'kadence/image' !== $name && 'core/image' !== $name ) {
		return $html;
	}
	if ( empty( $block['attrs']['bwLightbox'] ) ) {
		return $html;
	}

	$src = '';
	if ( ! empty( $block['attrs']['id'] ) ) {
		$full = wp_get_attachment_image_url( (int) $block['attrs']['id'], 'full' );
		if ( $full ) {
			$src = $full;
		}
	}
	if ( '' === $src && preg_match( '/<img[^>]+src="([^"]+)"/', $html, $m ) ) {
		$src = $m[1];
	}

	$count = 0;
	$out   = preg_replace_callback(
		'/<figure\b([^>]*)>/',
		function ( $mm ) use ( $src ) {
			if ( false !== strpos( $mm[1], 'data-bw-lb' ) ) {
				return $mm[0];
			}
			return '<figure' . $mm[1] . ' data-bw-lb="1" data-bw-lb-src="' . esc_url( $src ) . '">';
		},
		$html,
		1,
		$count
	);

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