对小工具进行了一些修复
This commit is contained in:
@@ -128,6 +128,10 @@ final class FormRenderer {
|
||||
|
||||
private static function select(string $id, string $name, array $field, $value): void {
|
||||
$multiple = !empty($field['multiple']);
|
||||
if (!is_array($field['options'] ?? [])) {
|
||||
self::remote_select($id, $name, $field, $value);
|
||||
return;
|
||||
}
|
||||
echo '<select class="widefat" id="' . esc_attr($id) . '" name="' . esc_attr($name) . ($multiple ? '[]' : '') . '"' . ($multiple ? ' multiple' : '') . '>';
|
||||
$values = $multiple ? array_map('strval', (array) $value) : [(string) $value];
|
||||
foreach ((array) ($field['options'] ?? []) as $key => $label) {
|
||||
@@ -136,6 +140,37 @@ final class FormRenderer {
|
||||
echo '</select>';
|
||||
}
|
||||
|
||||
private static function remote_select(string $id, string $name, array $field, $value): void {
|
||||
$values = array_values(array_unique(array_filter(array_map('absint', (array) $value))));
|
||||
$options = self::remote_selected_options($field, $values);
|
||||
$source = (string) ($field['options'] ?? '');
|
||||
$query_args = wp_json_encode((array) ($field['query_args'] ?? []));
|
||||
$min_length = max(0, (int) ($field['settings']['min_length'] ?? 0));
|
||||
$placeholder = (string) ($field['placeholder'] ?? __('输入关键词以搜索', 'aksrt'));
|
||||
echo '<select class="widefat aksrt-widget-remote-select" id="' . esc_attr($id) . '" name="' . esc_attr($name) . '[]" multiple data-option-source="' . esc_attr($source) . '" data-query-args="' . esc_attr((string) $query_args) . '" data-min-length="' . $min_length . '" data-placeholder="' . esc_attr($placeholder) . '">';
|
||||
foreach ($options as $option_id => $label) {
|
||||
echo '<option value="' . esc_attr((string) $option_id) . '" selected>' . esc_html($label) . '</option>';
|
||||
}
|
||||
echo '</select>';
|
||||
}
|
||||
|
||||
private static function remote_selected_options(array $field, array $values): array {
|
||||
$options = [];
|
||||
$source = (string) ($field['options'] ?? '');
|
||||
$query_args = (array) ($field['query_args'] ?? []);
|
||||
foreach ($values as $value) {
|
||||
$label = '';
|
||||
if ($source === 'post' && function_exists('get_the_title')) {
|
||||
$label = (string) get_the_title($value);
|
||||
} elseif ($source === 'categories' && function_exists('get_term')) {
|
||||
$term = get_term($value, sanitize_key((string) ($query_args['taxonomy'] ?? '')));
|
||||
if ($term && !is_wp_error($term)) $label = (string) $term->name;
|
||||
}
|
||||
$options[$value] = $label !== '' ? $label : '#' . $value;
|
||||
}
|
||||
return $options;
|
||||
}
|
||||
|
||||
private static function checkboxes(string $id, string $name, array $field, $value): void {
|
||||
$options = (array) ($field['options'] ?? []);
|
||||
if (!$options) {
|
||||
|
||||
Reference in New Issue
Block a user