''])); } /** @param mixed[] $args */ public static function textarea(array $args): string { return parent::textarea(self::daytidyFieldArgs($args)); } /** @param mixed[] $args */ public static function checkbox(array $args): string { return parent::checkbox(self::daytidyFieldArgs($args)); } /** @param mixed[] $args */ public static function checkboxes(array $args): string { if (!count($args)) return parent::checkboxes($args); $attributes = isset($args['attributes']) && is_array($args['attributes']) ? $args['attributes'] : []; $classes = array_values(array_unique(array_filter(preg_split('/\s+/', trim((string) ($attributes['class'] ?? ''))) ?: [], static function (string $class): bool { return $class !== '' && (Html::is_admin_context() || strpos($class, 'daytidy-admin-') !== 0); }))); $field_class = Html::get_daytidy_class('fields'); if (!in_array($field_class, $classes, true)) $classes[] = $field_class; $attributes['class'] = implode(' ', $classes); $args['attributes'] = $attributes; return parent::checkboxes($args); } /** @param mixed[] $args */ public static function editor(array $args): string { if (Html::is_admin_context() && !wp_doing_ajax()) return parent::editor($args); // wp_editor prints editor-buttons and its Dashicons dependency into its // HTML buffer. Public shortcode sanitization removes those link tags, // although WordPress has already marked the styles as done. Defer only // links produced during this editor render to the normal asset pipeline. $deferred = []; $defer = static function (string $tag, string $handle) use (&$deferred): string { $deferred[] = $handle; return ''; }; add_filter('style_loader_tag', $defer, PHP_INT_MAX, 2); try { return parent::editor($args); } finally { remove_filter('style_loader_tag', $defer, PHP_INT_MAX); $styles = wp_styles(); $styles->done = array_values(array_diff($styles->done, $deferred)); foreach ($deferred as $handle) wp_enqueue_style($handle); } } public static function requiredStar(): string { return '*'; } /** * @param mixed[] $args * @return string */ public static function switcher(array $args): string { if (!count($args)) return ''; $toggle = Arr::isset($args, 'toggle'); $toggle2 = Arr::isset($args, 'toggle2'); // Current Value $value = Arr::isset($args, 'value') ? (int) $args['value'] : 0; $disabled = !empty($args['disabled']); $admin = Html::is_admin_context(); return ''; } /** * @param mixed[] $args * @return string */ public static function imagepicker(array $args): string { if (!count($args)) return ''; $image_id = $args['value'] ?? ''; $image = $image_id ? wp_get_attachment_image($image_id, [400, 266]) : ''; $button_classes = Html::is_admin_context() ? ' daytidy-button daytidy-button--secondary' : ''; return '
'.Str::ternary($image, '').'
'; } /** * @param mixed[] $args * @return string */ public static function autosuggest(array $args): string { if (!count($args)) return ''; $source = sanitize_key((string) ($args['source'] ?? '')); $name = (string) ($args['name'] ?? ''); if (!$source || !$name) return ''; $id = (string) ($args['id'] ?? ('daytidy-autosuggest-' . md5($name . $source))); $class = trim('daytidy-autosuggest ' . (string) ($args['class'] ?? '')); $gap_class = Html::get_daytidy_class('fields-gap'); if (!in_array($gap_class, preg_split('/\s+/', $class) ?: [], true)) $class .= ' ' . $gap_class; $label = (string) ($args['label'] ?? ''); $placeholder = (string) ($args['placeholder'] ?? esc_html__('Search items', 'daytidy')); $min_characters = absint($args['min_characters'] ?? 2); $limit = absint($args['limit'] ?? 0); $multiple = $limit !== 1; $input_attributes = isset($args['input_attributes']) && is_array($args['input_attributes']) ? self::plainAttributes($args['input_attributes']) : ''; $values = isset($args['values']) && is_array($args['values']) ? array_map('absint', $args['values']) : []; $wrapper_attributes = self::plainAttributes([ 'class' => $class, 'data-name' => $name, 'data-source' => $source, 'data-action' => 'daytidy-autosuggest', 'data-nonce' => wp_create_nonce('daytidy-autosuggest'), 'data-min-characters' => $min_characters, 'data-limit' => $limit, ]); $selected = ''; $selected_count = 0; foreach (array_unique(array_filter($values)) as $value) { $selected .= Autosuggest::item( $name, (int) $value, Autosuggest::label($source, (int) $value), $multiple ); $selected_count++; if ($limit > 0 && $selected_count >= $limit) break; } return implode('', [ '', $label ? sprintf('', esc_attr($id), esc_html($label), Html::is_admin_context() ? Html::get_daytidy_class('fields-label', 'admin') . ' ' : 'wui-field__label ' . Html::get_daytidy_class('fields-label', 'fe') . ' ') : '', '', sprintf('
%s
', $selected), '', ]); } /** * @param array $attributes * @return string */ private static function plainAttributes(array $attributes): string { $output = ''; foreach ($attributes as $key => $value) { if ($value === null || $value === false) continue; if ($value === true) { $output .= ' ' . esc_attr((string) $key); continue; } if (!is_scalar($value)) continue; $output .= sprintf(' %s="%s"', esc_attr((string) $key), esc_attr((string) $value)); } return $output; } /** * @param mixed[] $args * @return string */ public static function copyableCode(array $args): string { if (!count($args)) return ''; $value = (string) ($args['value'] ?? ''); if ($value === '') return ''; $id = isset($args['id']) ? sprintf(' id="%s"', esc_attr((string) $args['id'])) : ''; $class = trim('daytidy-copyable-code ' . (string) ($args['class'] ?? '')); $copy_label = (string) ($args['copy_label'] ?? esc_html__('Copy to clipboard', 'daytidy')); $copied_label = (string) ($args['copied_label'] ?? esc_html__('Copied', 'daytidy')); $button_label = (string) ($args['button_label'] ?? ''); $button_content = $button_label !== '' ? esc_html($button_label) : ''; $button_variant = $button_label !== '' && ($args['button_variant'] ?? '') === 'neutral' ? 'neutral' : ($button_label !== '' ? 'secondary' : 'icon'); $button_classes = $button_variant === 'icon' ? 'wui-button wui-button--tertiary wui-button--icon' : 'wui-button wui-button--' . $button_variant; if (Html::is_admin_context()) $button_classes .= ' daytidy-button daytidy-button--' . ($button_variant === 'icon' ? 'tertiary daytidy-button--icon' : $button_variant); return sprintf( '%5$s', $id, esc_attr($class), esc_attr($copy_label), esc_attr($copied_label), esc_html($value), $button_content, esc_attr($button_classes) ); } /** * @param mixed[] $args * @return string */ public static function copyableTextarea(array $args): string { if (!count($args)) return ''; $value = (string) ($args['value'] ?? ''); if ($value === '') return ''; $id = (string) ($args['id'] ?? ('daytidy-copyable-textarea-' . md5($value))); $class = trim('daytidy-copyable-code daytidy-copyable-textarea ' . (string) ($args['class'] ?? '')); $copy_label = (string) ($args['copy_label'] ?? esc_html__('Copy to clipboard', 'daytidy')); $copied_label = (string) ($args['copied_label'] ?? esc_html__('Copied', 'daytidy')); $rows = max(3, absint($args['rows'] ?? 12)); return sprintf( '
', esc_attr($class), esc_attr($copy_label), esc_attr($copied_label), esc_attr($id), $rows, esc_textarea($value), Html::is_admin_context() ? ' daytidy-textarea ' . Html::get_daytidy_class('fields', 'admin') : ' ' . Html::get_daytidy_class('fields', 'fe'), Html::is_admin_context() ? ' daytidy-button daytidy-button--tertiary daytidy-button--icon' : '', Html::get_daytidy_class('icon') ); } /** * @param mixed[] $args * @return string */ public static function iconButton(array $args = []): string { if (!count($args)) return ''; $icon = (string) ($args['icon'] ?? ''); $label = (string) ($args['label'] ?? ''); if (!$icon || !$label) return ''; if (strpos($icon, 'wui-icon') !== false) { $icon = trim(str_replace(['daytidy-fe-icon', 'daytidy-admin-icon'], '', $icon)); $icon = (preg_replace('/\s+/', ' ', $icon) ?? $icon) . ' ' . Html::get_daytidy_class('icon'); } $attributes = isset($args['attributes']) && is_array($args['attributes']) ? $args['attributes'] : []; $attributes['type'] = (string) ($attributes['type'] ?? ($args['type'] ?? 'button')); $attributes['class'] = trim('wui-button wui-button--tertiary wui-button--icon ' . (Html::is_admin_context() ? 'daytidy-button daytidy-button--tertiary daytidy-button--icon ' : '') . (string) ($args['class'] ?? '')); $attributes['aria-label'] = (string) ($attributes['aria-label'] ?? $label); if (isset($args['title'])) { $attributes['title'] = (string) $args['title']; } $html_attributes = ''; foreach ($attributes as $key => $value) { if ($value === null || $value === false) continue; if ($value === true) { $html_attributes .= ' ' . esc_attr((string) $key); continue; } $html_attributes .= sprintf(' %s="%s"', esc_attr((string) $key), esc_attr((string) $value)); } return sprintf( '%3$s', $html_attributes, esc_attr($icon), esc_html($label) ); } public static function datepicker(array $args): string { if (!count($args)) return ''; return self::input($args); } /** * @param array $args * @return string */ public static function tzpicker(array $args = []): string { if (!count($args)) return ''; $selected = Arr::ternary($args, 'value', ''); $options = wp_timezone_choice($selected); $ex = explode(' ' . $options . ' '; } /** * @param array $args * @return string */ public static function spinner(array $args = []): string { if (!count($args)) return ''; $attributes = self::concat_attributes($args); $vertical = Arr::truthy($args, 'vertical'); $down = ' '; $up = ' '; if ($vertical) { $button1 = $up; $button2 = $down; } else { $button1 = $down; $button2 = $up; } return '
' . $button1 . ' ' . $button2 . '
'; } /** * @param mixed[] $args * @return string */ public static function password(array $args = []): string { return '
'.self::input($args, 'password').'
'; } /** * @param mixed[] $args * @return string */ public static function radio(array $args): string { if (!count($args)) return ''; $attributes = self::concat_attributes($args); $required = isset($args['required']) && $args['required']; $name = (string) ($args['name'] ?? ''); $id = (string) ($args['id'] ?? md5($name)); $value = isset($args['value']) ? (string) $args['value'] : ''; $options = isset($args['options']) && is_array($args['options']) ? $args['options'] : []; $items = ''; foreach ($options as $option_value => $label) { $option_id = $id . '-' . sanitize_html_class((string) $option_value); $items .= sprintf( '
  • ', esc_attr($option_id), esc_attr($name), esc_attr((string) $option_value), $value !== '' && $value === (string) $option_value ? 'checked' : '', trim($attributes), $required ? 'required' : '', esc_html((string) $label), Html::is_admin_context() ? Html::get_daytidy_class('fields-label', 'admin') : '', ' class="' . Html::get_daytidy_class('fields') . '"' ); } return '
      ' . $items . '
    '; } /** * @param array $args * @param string $type * @return string */ public static function button(array $args = [], $type = 'button'): string { if (!count($args)) return ''; // Attributes $attributes = self::concat_attributes($args); return ''; } /** * @param array $args * @return string */ public static function offerings(array $args): string { $offerings = get_posts([ 'post_type' => Offering::PT, 'posts_per_page' => -1, 'post_status' => 'publish' ]); $options = []; foreach ($offerings as $offering) { $options[$offering->ID] = $offering->post_title; } $args['options'] = $options; return self::select($args); } }