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.
This commit is contained in:
1 parent
dff9d7dc1c
commit
8e3b86a21b
3 files changed
+173
-91
No files matched your search
@@ -22,9 +22,19 @@ class WidgetIframe extends \WP_Widget {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $args
|
||||
* @param array<string,mixed> $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 '<div class="c-red muted-box mb20"><b>' . esc_html__('超级嵌入:', 'aksrt') . '</b>' .
|
||||
esc_html__('请配置 URL 或 iframe 代码', 'aksrt') . '</div>';
|
||||
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('/<iframe\b[^>]*>/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('/^<iframe\b/i', '<iframe class="lazyload aksrt-lazy"', $tag, 1);
|
||||
}
|
||||
}
|
||||
if (is_string($tag)) {
|
||||
$iframe_html = str_replace($m[0], $tag, $iframe_html);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$url = esc_url($raw);
|
||||
if ($url === '') {
|
||||
@@ -65,17 +95,23 @@ class WidgetIframe extends \WP_Widget {
|
||||
return;
|
||||
}
|
||||
$fs = $allow_fs ? ' allowfullscreen="allowfullscreen"' : '';
|
||||
$iframe_html = '<iframe class="aksrt-iframe-el" src="' . $url . '" loading="lazy" frameborder="0"' . $fs . '></iframe>';
|
||||
// zibll: class="lazyload" + data-src (not immediate src).
|
||||
$iframe_html = '<iframe class="lazyload aksrt-lazy aksrt-iframe-el" src="" data-src="' . $url . '" loading="lazy" frameborder="0" framespacing="0" border="0"' . $fs . '></iframe>';
|
||||
}
|
||||
|
||||
echo '<div class="aksrt-iframe-embed">';
|
||||
// zibll shell: mb20 > wp-block-embed is-type-video relative-h radius8 > padding-bottom.
|
||||
echo '<div class="mb20 aksrt-iframe-embed">';
|
||||
echo '<div class="wp-block-embed is-type-video relative-h radius8">';
|
||||
echo '<div class="aksrt-iframe-ratio" style="padding-bottom:' . esc_attr((string) $aspect) . '%">';
|
||||
echo $iframe_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
echo '</div></div>';
|
||||
echo '</div></div></div>';
|
||||
|
||||
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $instance
|
||||
*/
|
||||
public function form($instance): void {
|
||||
$title = (string) ($instance['title'] ?? '');
|
||||
$url = (string) ($instance['url'] ?? '');
|
||||
@@ -102,6 +138,11 @@ class WidgetIframe extends \WP_Widget {
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $new_instance
|
||||
* @param array<string,mixed> $old_instance
|
||||
* @return array<string,mixed>
|
||||
*/
|
||||
public function update($new_instance, $old_instance): array {
|
||||
return [
|
||||
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
|
||||
|
||||
Reference in new issue
Block a user