<?php
/**
 * Main theme/plugin file to initialize the framework.
 */

// Include the engine file.
require_once SIFENCY_TB_PATH . 'includes/functions/metabox.php';   
require_once SIFENCY_TB_PATH . 'includes/functions/metabox-helpers.php';   

/**
 * Initializes the Sifency Metabox framework.
 */
add_action( 'after_setup_theme', 'sifency_addons_custom_metaboxes_init' );
function sifency_addons_custom_metaboxes_init() {
	$metabox_definitions = sifency_addons_get_all_metabox_definitions();

	if ( ! empty( $metabox_definitions ) ) {
		new Sifency_Metabox( $metabox_definitions );
	}
}

/**
 * Dynamic loader for metabox definitions.
 *
 * Scans the /post-type/ subdirectories for `*-meta.php` files,
 * includes them, and merges the returned arrays into a single configuration array.
 *
 * @return array The combined array of all metabox definitions.
 */
function sifency_addons_get_all_metabox_definitions() {
	$all_definitions = [];
	$definition_files = glob( SIFENCY_TB_PATH . 'post-type/*/*-meta.php' );

	if ( empty( $definition_files ) ) {
		return [];
	}

	foreach ( $definition_files as $file ) {
		$definitions = include $file;

		if ( is_array( $definitions ) ) {
			$all_definitions = array_merge( $all_definitions, $definitions );
		}
	}

	return $all_definitions;
}