<?php
/**
 * Brentwood 100 — query helper + modal-body builder + cache invalidation.
 *
 * Powers the bw/hundred-grid block: one query over the `bw_hundred` CPT
 * (ordered by badge number via menu_order), a builder for the modal pop-up body
 * (title + number + gallery + story), and a transient bust on any edit so the
 * cached grid HTML stays fresh. The grid itself is cached in the block render.
 *
 * @package Kadence-Child
 */

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

/**
 * All published Brentwood 100 items, in badge-number order.
 *
 * @return WP_Post[]
 */
function bw_hundred_query() {
	if ( ! post_type_exists( 'bw_hundred' ) ) {
		return array();
	}
	$q = new WP_Query(
		array(
			'post_type'           => 'bw_hundred',
			'post_status'         => 'publish',
			'posts_per_page'      => -1,
			'orderby'             => array( 'menu_order' => 'ASC', 'title' => 'ASC' ),
			'no_found_rows'       => true,
			'ignore_sticky_posts' => true,
		)
	);
	return $q->posts;
}

/**
 * Ordered attachment IDs for an item's modal gallery: featured image first,
 * then the ACF gallery (de-duplicated).
 *
 * @param int $post_id Item ID.
 * @return int[]
 */
function bw_hundred_image_ids( $post_id ) {
	$ids = array();
	if ( has_post_thumbnail( $post_id ) ) {
		$ids[] = (int) get_post_thumbnail_id( $post_id );
	}
	foreach ( (array) get_field( 'hundred_gallery', $post_id ) as $gid ) {
		$gid = (int) $gid;
		if ( $gid && ! in_array( $gid, $ids, true ) ) {
			$ids[] = $gid;
		}
	}
	return $ids;
}

/**
 * The modal pop-up body for one item (cloned into the shared bw-tl modal).
 * Uses do_blocks() rather than the_content to avoid nesting the content filter.
 *
 * @param WP_Post $p Item.
 * @return string HTML.
 */
function bw_hundred_modal_html( $p ) {
	$num  = get_field( 'hundred_number', $p->ID );
	$imgs = '';
	foreach ( bw_hundred_image_ids( $p->ID ) as $gid ) {
		$imgs .= '<figure class="bw100-modal__figure">'
			. wp_get_attachment_image( $gid, 'large', false, array( 'class' => 'bw100-modal__img', 'loading' => 'lazy' ) )
			. '</figure>';
	}
	$body = do_blocks( $p->post_content );

	return '<article class="bw100-modal">'
		. ( ( '' !== $num && null !== $num ) ? '<span class="bw100-modal__num">' . esc_html( $num ) . '</span>' : '' )
		. '<h2 class="bw100-modal__title">' . esc_html( get_the_title( $p ) ) . '</h2>'
		. ( '' !== $imgs ? '<div class="bw100-modal__gallery">' . $imgs . '</div>' : '' )
		. '<div class="bw100-modal__body">' . $body . '</div>'
		. '</article>';
}

/**
 * Bust the cached grid HTML whenever an item is saved, trashed or deleted.
 */
function bw_hundred_flush_cache() {
	foreach ( array( 2, 3, 4, 5, 6 ) as $cols ) {
		delete_transient( 'bw_hundred_grid_c' . $cols );
	}
}
add_action( 'save_post_bw_hundred', 'bw_hundred_flush_cache' );
add_action( 'trashed_post', function ( $id ) {
	if ( 'bw_hundred' === get_post_type( $id ) ) {
		bw_hundred_flush_cache();
	}
} );
add_action( 'deleted_post', function ( $id ) {
	if ( 'bw_hundred' === get_post_type( $id ) ) {
		bw_hundred_flush_cache();
	}
} );
