feat: rewrite schema-driven widget system
This commit is contained in:
1 parent
6c6c462df8
commit
ad99ab53ce
28 files changed
+3489
No files matched your search
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace AKSRT\WidgetSystem;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
/**
|
||||
* Small HTML primitives shared by the renderer groups.
|
||||
*/
|
||||
final class RendererHelpers {
|
||||
public static function link(array $link): array {
|
||||
$url = esc_url((string) ($link['url'] ?? ''));
|
||||
$text = sanitize_text_field((string) ($link['text'] ?? ''));
|
||||
$target = ($link['target'] ?? '') === '_blank' ? '_blank' : '';
|
||||
return ['url' => $url, 'text' => $text, 'target' => $target];
|
||||
}
|
||||
|
||||
public static function link_attrs(array $link): string {
|
||||
$link = self::link($link);
|
||||
return $link['target'] === '_blank' ? ' target="_blank" rel="noopener noreferrer"' : '';
|
||||
}
|
||||
|
||||
public static function image(string $url, string $alt = '', string $class = ''): string {
|
||||
if ($url === '') {
|
||||
return '';
|
||||
}
|
||||
return '<img' . ($class !== '' ? ' class="' . esc_attr($class) . '"' : '')
|
||||
. ' src="' . esc_url($url) . '" alt="' . esc_attr($alt) . '" loading="lazy">';
|
||||
}
|
||||
|
||||
public static function lines(string $value): array {
|
||||
$lines = preg_split('/\r\n|\r|\n/', $value) ?: [];
|
||||
return array_values(array_filter(array_map('trim', $lines), static fn(string $line): bool => $line !== ''));
|
||||
}
|
||||
|
||||
public static function empty_state(string $message): string {
|
||||
if (function_exists('aksrt_get_widget_null')) {
|
||||
return aksrt_get_widget_null($message);
|
||||
}
|
||||
return '<div class="aksrt-widget__empty" role="status">' . esc_html($message) . '</div>';
|
||||
}
|
||||
|
||||
public static function card_media(array $item, string $class = ''): string {
|
||||
$title = (string) ($item['title'] ?? '');
|
||||
$link = self::link((array) ($item['link'] ?? []));
|
||||
$media = '';
|
||||
if (!empty($item['image_dark'])) {
|
||||
$media .= self::image((string) $item['image_dark'], $title, 'aksrt-media-card__image aksrt-media-card__image--dark');
|
||||
}
|
||||
$media .= self::image((string) ($item['image'] ?? ''), $title, 'aksrt-media-card__image');
|
||||
if (!empty($item['video'])) {
|
||||
$media .= '<video class="aksrt-media-card__video" src="' . esc_url((string) $item['video']) . '" muted loop playsinline preload="metadata"></video>';
|
||||
}
|
||||
$body = '<span class="aksrt-media-card__body"><strong>' . esc_html($title) . '</strong>';
|
||||
if (!empty($item['description'])) {
|
||||
$body .= '<small>' . esc_html((string) $item['description']) . '</small>';
|
||||
}
|
||||
$body .= '</span>';
|
||||
$tag = $link['url'] !== '' ? 'a' : 'div';
|
||||
$attrs = $link['url'] !== '' ? ' href="' . $link['url'] . '"' . self::link_attrs($link) : '';
|
||||
return '<' . $tag . ' class="aksrt-media-card ' . esc_attr($class) . '"' . $attrs . '><span class="aksrt-media-card__media">' . $media . '</span>' . $body . '</' . $tag . '>';
|
||||
}
|
||||
|
||||
public static function ids(string $value): array {
|
||||
return array_values(array_filter(array_map('absint', preg_split('/\s*,\s*/', $value) ?: [])));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user