mirror of
https://github.com/LamGC/wp-asciiplayer.git
synced 2025-04-29 14:17:32 +00:00
82 lines
2.0 KiB
PHP
82 lines
2.0 KiB
PHP
<?php
|
|
|
|
/*
|
|
Plugin Name: Asciicast Player
|
|
Plugin URI: https://github.com/LamGC/wp-asciiplayer
|
|
Description: AsciinemaPlayer for WordPress.
|
|
Version: 0.1.0
|
|
Author: LamGC
|
|
Author URI: https://blog.lamgc.moe
|
|
License: GNU GENERAL PUBLIC LICENSE Version 3.0
|
|
*/
|
|
|
|
const ASCIIPLAYER_TAG = "asciiplayer";
|
|
const ASCIIPLAYER_VERSION = '3.4.0';
|
|
|
|
// 是否使用本地资源, 如果可以的话, 建议使用 CDN.
|
|
add_option("ap_use_local_resources", false);
|
|
|
|
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>";
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
|
|
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',
|
|
plugin_dir_url(__FILE__) . '/asciiplayer/dist/bundle.js',
|
|
);
|
|
wp_enqueue_style(
|
|
'asciiplayer-adapter-css',
|
|
plugin_dir_url(__FILE__) . '/asciiplayer/dist/styles.css',
|
|
);
|
|
}
|
|
}
|
|
add_action( 'wp_enqueue_scripts', 'load_scripts' );
|