'; foreach ($fields as $field) { self::field($widget, $field, $instance[(string) ($field['id'] ?? '')] ?? ($field['default'] ?? ''), ''); } echo ''; } private static function field(SchemaWidget $widget, array $field, $value, string $parent): void { $type = (string) ($field['type'] ?? 'text'); $id = (string) ($field['id'] ?? ''); if (in_array($type, ['content', 'subheading', 'submessage'], true)) { self::display_field($field, $type); return; } if ($id === '') { return; } $key = $parent === '' ? $id : $parent . '[' . $id . ']'; $field_id = $widget->get_field_id(str_replace(['[', ']'], ['-', ''], $key)); $field_name = $widget->get_field_name($key); $dependency = !empty($field['dependency']) ? wp_json_encode($field['dependency']) : ''; echo '
'; if (!empty($field['label'])) { echo ''; } if (!empty($field['description'])) { echo '

' . wp_kses_post((string) $field['description']) . '

'; } switch ($type) { case 'switcher': self::switcher($field_id, $field_name, (bool) $value); break; case 'radio': case 'button_set': case 'palette': case 'image_select': self::radios($field_id, $field_name, (array) ($field['options'] ?? []), (string) $value, $type); break; case 'select': self::select($field_id, $field_name, $field, $value); break; case 'checkbox': self::checkboxes($field_id, $field_name, $field, $value); break; case 'terms': self::terms($field_id, $field_name, $field, $value); break; case 'number': case 'spinner': case 'slider': self::number($field_id, $field_name, $field, $value, $type); break; case 'textarea': $rows = max(1, (int) ($field['attributes']['rows'] ?? 4)); echo ''; break; case 'upload': self::upload($field_id, $field_name, (string) $value, (string) ($field['library'] ?? '')); break; case 'color': echo ''; break; case 'icon': echo '
'; break; case 'link': self::link($widget, $key, is_array($value) ? $value : []); break; case 'spacing': self::spacing($widget, $key, is_array($value) ? $value : []); break; case 'background': self::background($widget, $key, is_array($value) ? $value : []); break; case 'group': self::group($widget, $field, is_array($value) ? $value : [], $key); break; case 'fieldset': case 'accordion': self::nested($widget, $field, is_array($value) ? $value : [], $key); break; case 'text': default: echo ''; } echo '
'; } private static function display_field(array $field, string $type): void { $content = (string) ($field['content'] ?? $field['label'] ?? ''); echo '
' . wp_kses_post($content) . '
'; } private static function switcher(string $id, string $name, bool $value): void { echo ''; echo ''; } private static function radios(string $id, string $name, array $options, string $value, string $type): void { echo '
'; foreach ($options as $key => $label) { $option_id = $id . '-' . sanitize_html_class((string) $key); $image = is_array($label) ? (string) ($label['image'] ?? $label['url'] ?? '') : (string) $label; $text = is_array($label) ? (string) ($label['label'] ?? $label['title'] ?? $key) : (string) $label; if ($type === 'image_select' && $text === $image) $text = (string) $key; $body = $type === 'image_select' && filter_var($image, FILTER_VALIDATE_URL) ? '' . esc_attr($text) . '' . esc_html($text) . '' : esc_html($text); echo ''; } echo '
'; } private static function select(string $id, string $name, array $field, $value): void { $multiple = !empty($field['multiple']); echo ''; } private static function checkboxes(string $id, string $name, array $field, $value): void { $options = (array) ($field['options'] ?? []); if (!$options) { self::switcher($id, $name, (bool) $value); return; } $values = array_map('strval', (array) $value); foreach ($options as $key => $label) { echo ' '; } } private static function number(string $id, string $name, array $field, $value, string $type): void { $min = isset($field['min']) ? ' min="' . esc_attr((string) $field['min']) . '"' : ''; $max = isset($field['max']) ? ' max="' . esc_attr((string) $field['max']) . '"' : ''; $step = ' step="' . esc_attr((string) ($field['step'] ?? 1)) . '"'; $input_type = $type === 'slider' ? 'range' : 'number'; $unit = trim((string) ($field['unit'] ?? '')); echo '
' . esc_html((string) $value) . '' . ($unit !== '' ? '' . esc_html($unit) . '' : '') . '
'; } private static function terms(string $id, string $name, array $field, $value): void { $taxonomy = (string) ($field['taxonomy'] ?? 'category'); $terms = get_terms(['taxonomy' => $taxonomy, 'hide_empty' => false, 'number' => 500, 'orderby' => 'name']); if (is_wp_error($terms)) { $terms = []; } $selected = array_values(array_filter(array_map('absint', (array) $value))); $by_id = []; foreach ($terms as $term) { $by_id[(int) $term->term_id] = $term; } echo '
'; echo ''; echo '
'; foreach ($selected as $term_id) { if (!isset($by_id[$term_id])) { continue; } echo '' . esc_html((string) $by_id[$term_id]->name) . ''; } echo '
'; echo ''; echo '
'; foreach ($terms as $term) { $term_id = (int) $term->term_id; echo ''; } echo '
'; } private static function upload(string $id, string $name, string $value, string $library = ''): void { $library = in_array($library, ['image', 'video'], true) ? $library : ''; echo '
'; if ($value !== '') { echo ''; } echo ''; echo '
'; } private static function link(SchemaWidget $widget, string $key, array $value): void { echo ''; } private static function spacing(SchemaWidget $widget, string $key, array $value): void { echo '
'; foreach (['top' => '上', 'right' => '右', 'bottom' => '下', 'left' => '左'] as $edge => $label) { echo ''; } echo '
'; } private static function background(SchemaWidget $widget, string $key, array $value): void { echo '
'; self::plain_input($widget, $key . '[color]', (string) ($value['color'] ?? ''), 'text', '背景颜色'); self::plain_input($widget, $key . '[image]', (string) ($value['image'] ?? ''), 'url', '背景图片 URL'); foreach ([ 'position' => ['center center' => '居中', 'top center' => '顶部', 'bottom center' => '底部', 'center left' => '左侧', 'center right' => '右侧'], 'repeat' => ['no-repeat' => '不重复', 'repeat' => '平铺', 'repeat-x' => '水平平铺', 'repeat-y' => '垂直平铺'], 'attachment' => ['scroll' => '滚动', 'fixed' => '固定'], 'size' => ['cover' => '覆盖', 'contain' => '包含', 'auto' => '原始尺寸'], ] as $part => $options) { $name = $widget->get_field_name($key . '[' . $part . ']'); echo ''; } echo '
'; } private static function group(SchemaWidget $widget, array $field, array $rows, string $key): void { $token = '__index_' . substr(md5($key), 0, 8) . '__'; echo '
'; foreach ($rows as $index => $row) { self::group_row($widget, $field, is_array($row) ? $row : [], $key, (string) $index); } echo '
'; } private static function group_row(SchemaWidget $widget, array $field, array $row, string $key, string $index): void { $prefix = (string) ($field['item_label'] ?? $field['label'] ?? __('项目', 'aksrt')); $number = ctype_digit($index) ? ((int) $index + 1) : 0; $title = $prefix . ($number ? ' ' . $number : ''); if (!empty($row['title'])) $title .= ':' . strip_tags((string) $row['title']); echo '
' . esc_html($title) . '
'; foreach ((array) ($field['fields'] ?? []) as $child) { $child_id = (string) ($child['id'] ?? ''); self::field($widget, $child, $row[$child_id] ?? ($child['default'] ?? ''), $key . '[' . $index . ']'); } echo '
'; } private static function nested(SchemaWidget $widget, array $field, array $value, string $key): void { echo '
' . esc_html((string) ($field['label'] ?? '更多设置')) . '
'; foreach ((array) ($field['fields'] ?? []) as $child) { $child_id = (string) ($child['id'] ?? ''); self::field($widget, $child, $value[$child_id] ?? ($child['default'] ?? ''), $key); } echo '
'; } private static function plain_input(SchemaWidget $widget, string $key, string $value, string $type, string $placeholder): void { $name = $widget->get_field_name($key); $id = $widget->get_field_id(str_replace(['[', ']'], ['-', ''], $key)); echo ''; } }