58 lines
1.8 KiB
PHP
58 lines
1.8 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('aksrt_submit_render_term_checks')) :
|
|
function aksrt_submit_render_term_checks(array $terms, array $checked, string $name, int $parent = 0, int $depth = 0): void {
|
|
$children_map = [];
|
|
foreach ($terms as $term) {
|
|
if (!$term instanceof WP_Term) {
|
|
continue;
|
|
}
|
|
$pid = (int) $term->parent;
|
|
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="aksrt-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);
|
|
}
|
|
};
|
|
|
|
// If include list was used without hierarchy, still render flat.
|
|
if (empty($children_map) && !empty($terms)) {
|
|
foreach ($terms as $term) {
|
|
if (!$term instanceof WP_Term) {
|
|
continue;
|
|
}
|
|
$tid = (int) $term->term_id;
|
|
echo '<div><label class="aksrt-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>';
|
|
}
|
|
return;
|
|
}
|
|
|
|
$render($parent, $depth);
|
|
}
|
|
endif;
|