<?php
/**
 * Bridge the "Override H1 page title" ACF field (in the merged "Page Settings"
 * group) to the bw-dev plugin's post meta `_bw_dev_title_override`.
 *
 * Background: the H1 title override used to be a standalone bw-dev plugin meta
 * box. To consolidate ALL page-level settings into ONE ACF "Page Settings" box
 * (2026-07-20), the override is now an ACF text field (key
 * `field_bw_page_h1_override`, name `page_h1_override`) whose value is read from
 * and written to the SAME meta key the plugin already uses. This means:
 *   - No data migration — existing `_bw_dev_title_override` values keep working.
 *   - The front-end H1 is still rendered by the plugin's `the_title` filter
 *     (and the hero-card block), both untouched.
 *   - The plugin's own classic meta box is removed in brentwood-editor-layout.php.
 *
 * The ACF field never stores its own copy: update returns null so ACF deletes
 * `page_h1_override`, keeping `_bw_dev_title_override` the single source of truth.
 *
 * @package Kadence-Child
 */

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

const BW_TITLE_OVERRIDE_META    = '_bw_dev_title_override';
const BW_TITLE_OVERRIDE_ACF_KEY = 'field_bw_page_h1_override';

/**
 * The same limited HTML allow-list the bw-dev module uses, so the box, the REST
 * meta, and this ACF field all sanitize the override identically.
 */
function bw_title_override_allowed_html() {
	return array(
		'em'     => array(),
		'strong' => array(),
		'i'      => array(),
		'b'      => array(),
		'br'     => array(),
		'span'   => array( 'class' => array() ),
	);
}

/**
 * Resolve an ACF $post_id (which may be "123", 123, "term_5", "options", …) to
 * an integer post ID. Returns 0 for anything that isn't a post.
 */
function bw_title_override_post_id( $post_id ) {
	return is_numeric( $post_id ) ? (int) $post_id : 0;
}

/**
 * Post types where the bw-dev override actually renders (default: pages only;
 * posts/CPTs are opt-in via the bw-dev settings tab). Read from the plugin so
 * the field follows the same setting; falls back to pages if the plugin's API
 * isn't available.
 */
function bw_title_override_enabled_types() {
	if ( function_exists( 'bw_dev' ) ) {
		$types = bw_dev()->settings()->get( 'title_override', 'post_types', array( 'page' ) );
		if ( is_array( $types ) && $types ) {
			return array_map( 'strval', $types );
		}
	}
	return array( 'page' );
}

// PREPARE: hide the field on post types where the override wouldn't render, so
// it doesn't appear (and silently do nothing) on hero-bearing CPTs like staff
// or course. The merged group's location spans those CPTs for the hero/footer
// fields; this keeps the H1 override scoped like the old plugin box was.
add_filter(
	'acf/prepare_field/key=' . BW_TITLE_OVERRIDE_ACF_KEY,
	function ( $field ) {
		$pt = '';
		if ( function_exists( 'get_current_screen' ) ) {
			$screen = get_current_screen();
			if ( $screen && ! empty( $screen->post_type ) ) {
				$pt = (string) $screen->post_type;
			}
		}
		if ( '' === $pt ) {
			$pt = (string) get_post_type();
		}
		if ( '' !== $pt && ! in_array( $pt, bw_title_override_enabled_types(), true ) ) {
			return false; // hide the field
		}
		return $field;
	}
);

// LOAD: populate the ACF field from the real meta key.
add_filter(
	'acf/load_value/key=' . BW_TITLE_OVERRIDE_ACF_KEY,
	function ( $value, $post_id ) {
		$id = bw_title_override_post_id( $post_id );
		return $id ? (string) get_post_meta( $id, BW_TITLE_OVERRIDE_META, true ) : $value;
	},
	10,
	2
);

// UPDATE: write to the real meta key (sanitized), then return null so ACF does
// NOT persist its own copy under `page_h1_override`.
add_filter(
	'acf/update_value/key=' . BW_TITLE_OVERRIDE_ACF_KEY,
	function ( $value, $post_id ) {
		$id = bw_title_override_post_id( $post_id );
		if ( $id ) {
			$clean = wp_kses( trim( (string) $value ), bw_title_override_allowed_html() );
			if ( '' === $clean ) {
				delete_post_meta( $id, BW_TITLE_OVERRIDE_META );
			} else {
				update_post_meta( $id, BW_TITLE_OVERRIDE_META, $clean );
			}
		}
		return null;
	},
	10,
	2
);
