<?php
/**
 * Theme icons for the core Icon block
 *
 * Registers the theme's icon collection with the Icon API introduced in
 * WordPress 7.1, so the icons appear in the native Icon block picker under
 * a "Matchday" collection and can be referenced as `matchday/{name}`.
 *
 * The SVG files live in `/assets/icons`. The Icon API sanitises them with a
 * strict allow-list (`svg`, `path`, `polygon`; `fill`, `fill-rule`, `d`,
 * `transform`), so icons must be fill-based — stroke attributes are removed.
 *
 * @package   Matchday
 * @since     1.1.0
 * @version   1.1.0
 */

if (!function_exists('matchday_register_icons')):
	/**
	 * Registers the Matchday icon collection and its icons.
	 *
	 * Hooked to `init` at the default priority, after core registers its own
	 * collections (priority 0). Does nothing on WordPress < 7.1.
	 *
	 * @since 1.1.0
	 *
	 * @return void
	 */
	function matchday_register_icons() {
		if (
			!function_exists('wp_register_icon_collection') ||
			!function_exists('wp_register_icon')
		) {
			return;
		}

		/**
		 * Filters the icons registered by the theme.
		 *
		 * @since 1.1.0
		 *
		 * @param string[] $icons Icon labels keyed by icon name. Each name must
		 *                        match a file in `/assets/icons/{name}.svg`.
		 */
		$icons = apply_filters('matchday_icons', [
			'star' => __('Star', 'matchday'),
			'phone' => __('Phone', 'matchday'),
			'clipboard' => __('Clipboard', 'matchday'),
			'trophy' => __('Trophy', 'matchday'),
			'calendar-date-range' => __('Calendar Date Range', 'matchday'),
			'arrow-right' => __('Arrow Right', 'matchday'),
		]);

		if (empty($icons)) {
			return;
		}

		wp_register_icon_collection('matchday', [
			'label' => __('Matchday', 'matchday'),
			'description' => __('Icons bundled with the Matchday theme.', 'matchday'),
		]);

		foreach ($icons as $name => $label) {
			wp_register_icon("matchday/{$name}", [
				'label' => $label,
				'file_path' => get_theme_file_path("/assets/icons/{$name}.svg"),
			]);
		}
	}
endif;
add_action('init', 'matchday_register_icons');
