<?php
/**
 * Footer polish — two client-requested fixes (2026-07-20).
 *
 * The footer is Kadence element #22041. Its top row selector is stable per
 * block: `.kb-row-layout-id22041_daf271-0f`. The row reserves 350px of bottom
 * padding for a per-page footer image (ACF field `footer_image_background`, an
 * attachment ID, positioned bottom-centre). Pages with no image therefore show
 * a large empty gap. The row also carries a Kadence overlay layer
 * (`.kt-row-layout-overlay`) that ships DISABLED (opacity:0) — it sits above
 * the background image and below the content, exactly where a readability tint
 * belongs.
 *
 * Fix 1 — collapse the 350px gap on pages that have NO footer image.
 * Fix 2 — on pages WITH a footer image, enable a blue→transparent overlay
 *          across the top so the nav/contact/social text stays readable where a
 *          tall image rises into it. The blue is the footer's own palette7
 *          (#dcf2fa); it fades to fully transparent lower down so the image's
 *          subject still shows.
 *
 * Done in the child theme (not the footer's block JSON) so it is
 * version-controlled, trivially revertible, and survives a footer re-import.
 *
 * @package Kadence-Child
 */

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

/**
 * The footer row's stable Kadence layout-id selector. If the Footer element
 * (#22041) is ever rebuilt from scratch this id changes — update it here.
 */
const BW_FOOTER_ROW = '.kb-row-layout-id22041_daf271-0f';

/**
 * Capture the main-query page id once, on the `wp` action, while the main
 * query is clean. By the time `body_class` runs (at the <body> tag) the
 * queried object can be clobbered by a secondary WP_Query during the header
 * render, so get_queried_object_id() is unreliable there.
 */
function bw_footer_capture_id() {
	$GLOBALS['_bw_footer_qid'] = (int) get_queried_object_id();
}
add_action( 'wp', 'bw_footer_capture_id' );

/**
 * Whether the current request's page has a footer background image
 * (ACF `footer_image_background`, an attachment ID). Resolved once, cached in
 * a static for the rest of the request.
 *
 * The meta is read straight from the DB with $wpdb rather than
 * get_post_meta(): the post-meta object cache was observed returning this
 * field empty on some requests for a page that definitely has it set. A direct
 * indexed lookup is stable and effectively free (one row, once per request).
 */
function bw_footer_has_image() {
	static $has = null;
	if ( null !== $has ) {
		return $has;
	}
	global $wpdb;
	$id = isset( $GLOBALS['_bw_footer_qid'] )
		? (int) $GLOBALS['_bw_footer_qid']
		: (int) get_queried_object_id();
	$val = '';
	if ( $id ) {
		$val = (string) $wpdb->get_var(
			$wpdb->prepare(
				"SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = 'footer_image_background' ORDER BY meta_id LIMIT 1",
				$id
			)
		);
	}
	$has = ( '' !== $val && '0' !== $val );
	return $has;
}

/**
 * Tag <body> so the CSS below can branch on image / no-image.
 */
function bw_footer_body_class( $classes ) {
	$classes[] = bw_footer_has_image() ? 'bw-footer-img' : 'bw-footer-noimg';
	return $classes;
}
add_filter( 'body_class', 'bw_footer_body_class' );

/**
 * Emit the footer polish CSS. Uses !important because it overrides Kadence's
 * own generated inline rules (which are non-!important, so this always wins
 * regardless of source order).
 */
function bw_footer_polish_css() {
	$row = BW_FOOTER_ROW;
	echo '<style id="bw-footer-polish">';

	// Fix 1: no image -> collapse the reserved 350px bottom gap to a tidy value.
	echo 'body.bw-footer-noimg ' . $row . ' > .kt-row-column-wrap{'
		. 'padding-bottom:40px !important;}';

	// Fix 2: with an image -> blue-to-transparent readability overlay. Solid
	// footer-blue across the top third (behind the nav/contact/social text),
	// fading to clear by ~66% so the lower image (its subject) is unobstructed.
	// The overlay sits above the image and below the content, so it never dims
	// the text itself. Tuning: raise the two stops to strengthen (more blue),
	// lower them to reveal more of the image.
	echo 'body.bw-footer-img ' . $row . ' > .kt-row-layout-overlay{'
		. 'opacity:1 !important;'
		. 'background:linear-gradient(180deg,'
		. 'var(--global-palette7,#dcf2fa) 0%,'
		. 'var(--global-palette7,#dcf2fa) 30%,'
		. 'rgba(220,242,250,0) 66%) !important;}';

	echo '</style>';
}
add_action( 'wp_head', 'bw_footer_polish_css', 20 );
