Files
wordpress-theme-aksrt/templates/parts/submit/term-checks.php
T

53 lines
1.5 KiB
PHP

<?php
/**
* Hierarchical multi-check term list.
*
* @param WP_Term[] $terms
* @param int[] $checked
* @param string $name
* @param int $parent
* @param int $depth
*/
defined('ABSPATH') || exit;
if (!function_exists('jio_submit_render_term_checks')) :
function jio_submit_render_term_checks(array $terms, array $checked, string $name, int $parent = 0, int $depth = 0): void {
$children_map = [];
$present = [];
foreach ($terms as $term) {
if ($term instanceof WP_Term) {
$present[(int) $term->term_id] = true;
}
}
foreach ($terms as $term) {
if (!$term instanceof WP_Term) {
continue;
}
$pid = (int) $term->parent;
if ($pid !== 0 && !isset($present[$pid])) {
$pid = 0;
}
if (!isset($children_map[$pid])) {
$children_map[$pid] = [];
}
$children_map[$pid][] = $term;
}
$render = static function (int $pid, int $d) use (&$render, $children_map, $checked, $name): void {
if (empty($children_map[$pid])) {
return;
}
foreach ($children_map[$pid] as $term) {
$tid = (int) $term->term_id;
$pad = $d > 0 ? ' style="margin-left:' . (int) ($d * 16) . 'px"' : '';
echo '<div' . $pad . '><label class="jio-submit-check">';
echo '<input type="checkbox" name="' . esc_attr($name) . '[]" value="' . esc_attr((string) $tid) . '"' . (in_array($tid, $checked, true) ? ' checked' : '') . '>';
echo '<span>' . esc_html($term->name) . '</span></label></div>';
$render($tid, $d + 1);
}
};
$render($parent, $depth);
}
endif;