Route friend links through shared links_box styles; iframe embeds use lazyload data-src and pagination.js now hydrates iframe/video lazies.
155 lines
5.7 KiB
PHP
155 lines
5.7 KiB
PHP
<?php
|
|
/**
|
|
* Super embed / iframe widget (zibll zib_widget_ui_iframe).
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
|
|
namespace AKSRT\Widgets;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
class WidgetIframe extends \WP_Widget {
|
|
|
|
public function __construct() {
|
|
parent::__construct(
|
|
'aksrt_iframe',
|
|
esc_html__('AKSRT 超级嵌入', 'aksrt'),
|
|
[
|
|
'description' => esc_html__('嵌入在线视频/音乐播放器或任意 iframe 内容。', 'aksrt'),
|
|
'classname' => 'widget_aksrt_iframe',
|
|
]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @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)));
|
|
$allow_fs = !empty($instance['allowfullscreen']);
|
|
|
|
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
$title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base);
|
|
if ($title !== '') {
|
|
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
}
|
|
|
|
$iframe_html = '';
|
|
if (stripos($raw, '<iframe') !== false && stripos($raw, '</iframe>') !== false) {
|
|
$iframe_html = wp_kses(
|
|
$raw,
|
|
[
|
|
'iframe' => [
|
|
'src' => true,
|
|
'width' => true,
|
|
'height' => true,
|
|
'frameborder' => true,
|
|
'allow' => true,
|
|
'allowfullscreen' => true,
|
|
'loading' => true,
|
|
'class' => true,
|
|
'style' => true,
|
|
'referrerpolicy' => true,
|
|
'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 === '') {
|
|
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
|
return;
|
|
}
|
|
$fs = $allow_fs ? ' allowfullscreen="allowfullscreen"' : '';
|
|
// 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>';
|
|
}
|
|
|
|
// 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></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'] ?? '');
|
|
$aspect = (int) ($instance['aspect'] ?? 55);
|
|
?>
|
|
<p>
|
|
<label><?php esc_html_e('标题', 'aksrt'); ?>
|
|
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr($title); ?>">
|
|
</label>
|
|
</p>
|
|
<p>
|
|
<label><?php esc_html_e('嵌入地址或 iframe 代码', 'aksrt'); ?>
|
|
<textarea class="widefat" rows="4" name="<?php echo esc_attr($this->get_field_name('url')); ?>"><?php echo esc_textarea($url); ?></textarea>
|
|
</label>
|
|
</p>
|
|
<p>
|
|
<label><?php esc_html_e('高度占比 %', 'aksrt'); ?>
|
|
<input class="tiny-text" type="number" min="20" max="300" step="5" name="<?php echo esc_attr($this->get_field_name('aspect')); ?>" value="<?php echo esc_attr((string) $aspect); ?>">
|
|
</label>
|
|
</p>
|
|
<p>
|
|
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('allowfullscreen')); ?>" value="1" <?php checked(!empty($instance['allowfullscreen'])); ?>> <?php esc_html_e('允许全屏', 'aksrt'); ?></label>
|
|
</p>
|
|
<?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'] ?? '')),
|
|
'url' => trim((string) ($new_instance['url'] ?? '')),
|
|
'aspect' => max(20, min(300, (int) ($new_instance['aspect'] ?? 55))),
|
|
'allowfullscreen' => !empty($new_instance['allowfullscreen']) ? 1 : 0,
|
|
];
|
|
}
|
|
}
|