Refresh player with native block editor preview and local assets

Preserve shortcode rendering, normalize options, bundle the player locally,
and add reproducible packaging and WordPress integration tests.
This commit is contained in:
2026-10-12 17:56:08 +08:00
parent 40e2bf0733
commit f04600c37b
90 changed files with 28835 additions and 2090 deletions
+27 -93
View File
@@ -1,100 +1,34 @@
<?php
/**
* Plugin Name: Asciicast Player
* Plugin URI: https://github.com/LamGC/wp-asciiplayer
* Description: Add interactive terminal recordings to posts and pages with a block or shortcode.
* Version: 1.1.0
* Requires at least: 6.3
* Requires PHP: 7.4
* Author: LamGC
* Author URI: https://blog.lamgc.moe
* License: GPL-3.0-only
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
* Text Domain: wp-asciiplayer
*
* @package LamGC\Asciiplayer
*/
/*
Plugin Name: Asciicast Player
Plugin URI: https://github.com/LamGC/wp-asciiplayer
Description: AsciinemaPlayer for WordPress.
Version: 1.0.0
Author: LamGC
Author URI: https://blog.lamgc.moe
License: GNU GENERAL PUBLIC LICENSE Version 3.0
*/
namespace LamGC\Asciiplayer;
const PLUGIN_VERSION = '1.0.1';
const ASCIIPLAYER_TAG = 'asciiplayer';
const AP_OPTION_USE_LOCAL_RESOURCES = 'ap_use_local_resources';
// 是否使用本地资源, 如果可以的话, 建议使用 CDN.
add_option(AP_OPTION_USE_LOCAL_RESOURCES, false, $autoload = true);
function handle_asciiplayer_code($attr = [], string $content = null): string
{
if (!isset($content)) {
return '';
}
$content = trim($content);
if (!is_array(wp_parse_url($content))) {
return '';
}
$opts = is_assoc($attr) ? $attr : [];
set_default_value($opts, 'preload', true);
$style = null;
if (isset($opts['style']) && is_string($opts['style'])) {
$style = $opts['style'];
}
unset($opts['style']);
$opts_json = urlencode_deep(json_encode($opts));
return "<div
class=\"asciiplayer-container\"
data-ap-src=\"$content\"
data-ap-opts=\"$opts_json\"
data-ap-style=\"$style\"
></div>";
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
function is_assoc($arr): bool
{
if (!is_array($arr)) return false;
if (array() === $arr) return false;
return array_keys($arr) !== range(0, count($arr) - 1);
}
const VERSION = '1.1.0';
const PLUGIN_FILE = __FILE__;
const SHORTCODE = 'asciiplayer';
require_once __DIR__ . '/includes/options.php';
require_once __DIR__ . '/includes/render.php';
require_once __DIR__ . '/includes/assets.php';
function set_default_value(array &$array, string $key, $value)
{
if (!isset($array[$key])) {
$array[$key] = $value;
}
}
add_shortcode(ASCIIPLAYER_TAG, 'handle_asciiplayer_code');
function load_scripts()
{
if (!is_single()) {
return;
}
global $post;
if (has_shortcode($post->post_content, ASCIIPLAYER_TAG)) {
wp_enqueue_script(
'asciiplayer-adapter-js',
get_adapter_url() . '/dist/bundle.js',
array(),
PLUGIN_VERSION
);
wp_enqueue_style(
'asciiplayer-adapter-css',
get_adapter_url() . '/dist/styles.css',
array(),
PLUGIN_VERSION
);
}
}
add_action('wp_enqueue_scripts', 'load_scripts');
function get_adapter_url(): string
{
$use_local_resources = boolval(get_option(AP_OPTION_USE_LOCAL_RESOURCES, true));
$version = PLUGIN_VERSION;
if ($use_local_resources) {
return plugin_dir_url(__FILE__) . '/asciiplayer';
} else {
return "https://cdn.jsdelivr.net/npm/wp-asciiplayer-adapter@$version";
}
}
add_action( 'init', __NAMESPACE__ . '\\register_plugin' );
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\\enqueue_content_assets' );
add_action( 'wp_footer', __NAMESPACE__ . '\\print_late_styles', 5 );