From 8e3b86a21be5f193943de99b99a0d7c330e97c35 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 21 Jul 2026 08:59:27 +0800 Subject: [PATCH] feat(widgets): FriendLinks aksrt_links_box + iframe lazy data-src Route friend links through shared links_box styles; iframe embeds use lazyload data-src and pagination.js now hydrates iframe/video lazies. --- assets/js/core/pagination.js | 88 ++++++++-------- src/AKSRT/Widgets/FriendLinksWidget.php | 129 +++++++++++++++--------- src/AKSRT/Widgets/WidgetIframe.php | 47 ++++++++- 3 files changed, 173 insertions(+), 91 deletions(-) diff --git a/assets/js/core/pagination.js b/assets/js/core/pagination.js index cd415ede..6c63914a 100644 --- a/assets/js/core/pagination.js +++ b/assets/js/core/pagination.js @@ -275,23 +275,49 @@ }); } + function activateLazyEl(el) { + var src = el.dataset.src; + if (!src) return; + var tag = (el.tagName || "").toLowerCase(); + if (tag === "iframe" || tag === "video") { + el.src = src; + el.classList.remove("aksrt-lazy", "lazyload"); + el.classList.add("aksrt-loaded", "lazyloaded"); + return; + } + // img path with optional skeleton + decode + var tempImg = new Image(); + tempImg.onload = function () { + el.src = src; + el.classList.remove("aksrt-lazy", "lazyload"); + el.classList.add("aksrt-loaded", "lazyloaded"); + var skeleton = el.previousElementSibling; + if (skeleton && skeleton.classList.contains("aksrt-skeleton")) { + skeleton.remove(); + } + }; + tempImg.onerror = function () { + el.classList.remove("aksrt-lazy", "lazyload"); + el.classList.add("aksrt-error"); + var skeleton = el.previousElementSibling; + if (skeleton && skeleton.classList.contains("aksrt-skeleton")) { + skeleton.remove(); + } + }; + tempImg.src = src; + } + function initLazyLoad(root) { var scope = root || document; - var lazyImages = scope.querySelectorAll("img.aksrt-lazy"); - if (!lazyImages.length) return; + // Images + iframes/videos that use data-src (zibll lazyload / aksrt-lazy). + var lazyEls = scope.querySelectorAll( + "img.aksrt-lazy, iframe.aksrt-lazy, iframe.lazyload, video.aksrt-lazy, video.lazyload" + ); + if (!lazyEls.length) return; if (!("IntersectionObserver" in window)) { - lazyImages.forEach(function (img) { - var src = img.dataset.src; - if (src) { - img.src = src; - img.classList.remove("aksrt-lazy"); - img.classList.add("aksrt-loaded"); - } - var skeleton = img.previousElementSibling; - if (skeleton && skeleton.classList.contains("aksrt-skeleton")) { - skeleton.remove(); - } + lazyEls.forEach(function (el) { + activateLazyEl(el); }); return; } @@ -299,43 +325,21 @@ var observer = new IntersectionObserver(function (entries) { entries.forEach(function (entry) { if (!entry.isIntersecting) return; - - var img = entry.target; - var src = img.dataset.src; - if (!src) { - observer.unobserve(img); + var el = entry.target; + if (!el.dataset.src) { + observer.unobserve(el); return; } - - var tempImg = new Image(); - tempImg.onload = function () { - img.src = src; - img.classList.remove("aksrt-lazy"); - img.classList.add("aksrt-loaded"); - var skeleton = img.previousElementSibling; - if (skeleton && skeleton.classList.contains("aksrt-skeleton")) { - skeleton.remove(); - } - }; - tempImg.onerror = function () { - img.classList.remove("aksrt-lazy"); - img.classList.add("aksrt-error"); - var skeleton = img.previousElementSibling; - if (skeleton && skeleton.classList.contains("aksrt-skeleton")) { - skeleton.remove(); - } - }; - tempImg.src = src; - - observer.unobserve(img); + activateLazyEl(el); + observer.unobserve(el); }); }, { rootMargin: "200px 0px", threshold: 0.01 }); - lazyImages.forEach(function (img) { - observer.observe(img); + lazyEls.forEach(function (el) { + observer.observe(el); }); } diff --git a/src/AKSRT/Widgets/FriendLinksWidget.php b/src/AKSRT/Widgets/FriendLinksWidget.php index b4b9de51..6818da21 100644 --- a/src/AKSRT/Widgets/FriendLinksWidget.php +++ b/src/AKSRT/Widgets/FriendLinksWidget.php @@ -1,6 +1,6 @@ esc_html__('显示友情链接(设置项或 WordPress 书签)。', 'aksrt'), + 'description' => esc_html__('友情链接:card / bigcard / image / simple(共享 aksrt_links_box)。', 'aksrt'), + 'classname' => 'widget_aksrt_friend_links', ]); } + /** + * @param array $args + * @param array $instance + */ public function widget($args, $instance): void { echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped $title = apply_filters('widget_title', (string) ($instance['title'] ?? __('友情链接', 'aksrt')), $instance, $this->id_base); - $show_icon = !empty($instance['show_icon']) || !isset($instance['show_icon']); - $columns = max(1, min(4, (int) ($instance['columns'] ?? 2))); $source = sanitize_key((string) ($instance['source'] ?? 'settings')); $go_link = !empty($instance['go_link']); $limit = max(1, min(100, (int) ($instance['limit'] ?? 24))); + $type = sanitize_key((string) ($instance['type'] ?? 'card')); + if (!in_array($type, ['card', 'bigcard', 'image', 'simple'], true)) { + $type = 'card'; + } + $blank = !isset($instance['blank']) || !empty($instance['blank']); + $nofollow = !isset($instance['nofollow']) || !empty($instance['nofollow']); + $show_box = !empty($instance['show_box']) || !isset($instance['show_box']); if ($title) { echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } - $links = $this->resolve_links($source, $instance, $limit); - if (!$links) { + $bookmarks = $this->resolve_bookmarks($source, $instance, $limit); + if (!$bookmarks) { echo function_exists('aksrt_get_widget_null') ? aksrt_get_widget_null(__('暂无友情链接', 'aksrt')) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped : '

' . esc_html__('暂无友情链接', 'aksrt') . '

'; echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped return; } - ?> - - ' . esc_html($name) . ''; + } + } + echo ''; + echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } /** + * Normalize to get_bookmarks-like objects/arrays for aksrt_links_box. + * * @param array $instance - * @return array> + * @return array */ - private function resolve_links(string $source, array $instance, int $limit): array { + private function resolve_bookmarks(string $source, array $instance, int $limit): array { if ($source === 'bookmarks') { $bm_args = [ 'orderby' => sanitize_key((string) ($instance['orderby'] ?? 'name')) ?: 'name', @@ -76,29 +86,35 @@ class FriendLinksWidget extends \WP_Widget { 'limit' => $limit, ]; $cats = trim((string) ($instance['cats'] ?? '')); - if ($cats !== '') { + if ($cats !== '' && $cats !== 'all') { $bm_args['category'] = $cats; } - $out = []; - foreach (get_bookmarks($bm_args) as $b) { - $out[] = [ - 'name' => (string) $b->link_name, - 'url' => (string) $b->link_url, - 'description' => (string) $b->link_description, - 'icon' => (string) $b->link_image, - ]; - } - return $out; + return get_bookmarks($bm_args) ?: []; } $links = class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::get_friend_links() : []; if (!is_array($links) || !$links) { - // Fallback to WP bookmarks when settings empty. - return $this->resolve_links('bookmarks', $instance, $limit); + return $this->resolve_bookmarks('bookmarks', $instance, $limit); } - return array_slice(array_values($links), 0, $limit); + $out = []; + foreach (array_slice(array_values($links), 0, $limit) as $link) { + if (!is_array($link)) { + continue; + } + $out[] = (object) [ + 'link_name' => (string) ($link['name'] ?? ''), + 'link_url' => (string) ($link['url'] ?? ''), + 'link_description' => (string) ($link['description'] ?? $link['desc'] ?? ''), + 'link_image' => (string) ($link['icon'] ?? $link['image'] ?? ''), + 'link_target' => '_blank', + ]; + } + return $out; } + /** + * @param array $instance + */ public function form($instance): void { ?>

@@ -115,8 +131,13 @@ class FriendLinksWidget extends \WP_Widget {

-

-
- +
+
+
+

$new_instance + * @param array $old_instance + * @return array + */ public function update($new_instance, $old_instance): array { + $type = sanitize_key((string) ($new_instance['type'] ?? 'card')); + if (!in_array($type, ['card', 'bigcard', 'image', 'simple'], true)) { + $type = 'card'; + } return [ 'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')), 'source' => ($new_instance['source'] ?? '') === 'bookmarks' ? 'bookmarks' : 'settings', - 'columns' => max(1, min(4, (int) ($new_instance['columns'] ?? 2))), + 'type' => $type, 'limit' => max(1, min(100, (int) ($new_instance['limit'] ?? 24))), 'cats' => sanitize_text_field((string) ($new_instance['cats'] ?? '')), - 'show_icon' => !empty($new_instance['show_icon']) ? 1 : 0, + 'blank' => !empty($new_instance['blank']) ? 1 : 0, + 'nofollow' => !empty($new_instance['nofollow']) ? 1 : 0, 'go_link' => !empty($new_instance['go_link']) ? 1 : 0, + 'show_box' => !empty($new_instance['show_box']) ? 1 : 0, 'orderby' => sanitize_key((string) ($new_instance['orderby'] ?? 'name')), 'order' => strtoupper((string) ($new_instance['order'] ?? 'ASC')) === 'DESC' ? 'DESC' : 'ASC', + // Keep legacy keys so old instances don't explode. + 'columns' => max(1, min(4, (int) ($new_instance['columns'] ?? 2))), + 'show_icon' => !empty($new_instance['show_icon']) ? 1 : 0, ]; } } diff --git a/src/AKSRT/Widgets/WidgetIframe.php b/src/AKSRT/Widgets/WidgetIframe.php index eaeeb2cc..cd2d04d9 100644 --- a/src/AKSRT/Widgets/WidgetIframe.php +++ b/src/AKSRT/Widgets/WidgetIframe.php @@ -22,9 +22,19 @@ class WidgetIframe extends \WP_Widget { ); } + /** + * @param array $args + * @param array $instance + */ public function widget($args, $instance): void { $raw = trim((string) ($instance['url'] ?? '')); if ($raw === '') { + if (current_user_can('manage_options')) { + echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + echo '
' . esc_html__('超级嵌入:', 'aksrt') . '' . + esc_html__('请配置 URL 或 iframe 代码', 'aksrt') . '
'; + echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + } return; } $aspect = max(20, min(300, (int) ($instance['aspect'] ?? 55))); @@ -55,9 +65,29 @@ class WidgetIframe extends \WP_Widget { 'title' => true, 'sandbox' => true, 'data-src' => true, + 'framespacing' => true, + 'border' => true, ], ] ); + // Prefer lazy: if iframe has src but no data-src, convert. + if (preg_match('/]*>/i', $iframe_html, $m) + && stripos($m[0], 'data-src=') === false + && preg_match('/\ssrc=["\']([^"\']+)["\']/i', $m[0], $sm) + ) { + $src = $sm[1]; + $tag = preg_replace('/\ssrc=["\'][^"\']*["\']/i', ' src="" data-src="' . esc_url($src) . '"', $m[0], 1); + if (is_string($tag) && stripos($tag, 'lazyload') === false) { + if (preg_match('/\sclass=["\']([^"\']*)["\']/', $tag)) { + $tag = preg_replace('/\sclass=["\']([^"\']*)["\']/', ' class="$1 lazyload aksrt-lazy"', $tag, 1); + } else { + $tag = preg_replace('/^'; + // zibll: class="lazyload" + data-src (not immediate src). + $iframe_html = ''; } - echo '
'; + // zibll shell: mb20 > wp-block-embed is-type-video relative-h radius8 > padding-bottom. + echo '
'; + echo '
'; echo '
'; echo $iframe_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped - echo '
'; + echo '
'; echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } + /** + * @param array $instance + */ public function form($instance): void { $title = (string) ($instance['title'] ?? ''); $url = (string) ($instance['url'] ?? ''); @@ -102,6 +138,11 @@ class WidgetIframe extends \WP_Widget { $new_instance + * @param array $old_instance + * @return array + */ public function update($new_instance, $old_instance): array { return [ 'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),