Files
wp-asciiplayer/includes/render.php
T
LamGC 85e5ffa0ec
Build and WordPress integration / PHP 7.4 syntax and regression tests (push) Canceled after 0s
Build and WordPress integration / PHP 8.3 syntax and regression tests (push) Canceled after 0s
Build and WordPress integration / WordPress 6.3 / PHP 7.4 (push) Canceled after 0s
Build and WordPress integration / WordPress latest / PHP 8.2 (push) Canceled after 0s
Refresh wp-asciiplayer with native block editor playback (#36)
* 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.

* Fix emitted frontend boot and verify bundled runtime initialization

* Record verified WordPress 7.1.3 compatibility

* Use WordPress readme major-version compatibility format

* Add responsive player layout, captions and native terminal sizing

* Keep editor caption geometry stable during block selection

* Respect WordPress editor gutters with border-box sizing

* Improve automatic previews and toolbar terminal sizing (1.2.1)

* Show native cast dimensions and add Media Library recordings (1.2.2)

* Fix Media Library test storage and scope sidebar selectors

* Document bounded temporary-upload stream exceptions

* Move block explanations into collapsed sidebar help (1.2.3)

* Add bundled English and Simplified Chinese UI translations (1.1.0-dev)

* Use native RichText labels and supported Chinese HTML language tags in locale tests
2026-10-14 15:56:05 +08:00

85 lines
3.2 KiB
PHP

<?php
/**
* Safe, shared rendering for shortcode and block content.
*
* @package LamGC\Asciiplayer
*/
namespace LamGC\Asciiplayer;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Render one player. Invalid/missing sources produce no frontend markup.
*
* @param mixed $attributes Player attributes.
* @param string|null $content Optional legacy shortcode source.
* @param bool $block Whether WordPress block wrapper supports apply.
* @return string
*/
function render_player( $attributes = array(), $content = null, $block = false ) {
$attributes = normalize_attributes( $attributes );
$source = sanitize_source( null !== $content ? $content : ( $attributes['source'] ?? '' ) );
if ( '' === $source ) {
return '';
}
$options = wp_json_encode( player_options( $attributes ), JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT );
if ( false === $options ) {
return '';
}
enqueue_player_assets();
$style = player_style( $attributes );
// The mount point owns only the player. Captions must stay outside it so
// upstream disposal/reinitialization cannot remove author-written content.
$mount_style = $style;
$figure_style = '';
if ( $block ) {
// Keep height and other legacy player-container declarations on the
// mount point. Only width moves to the figure, so captions add height.
if ( preg_match_all( '/(?:^|;)\s*width\s*:\s*([^;]+)/i', $style, $widths ) ) {
$figure_style = 'width:' . end( $widths[1] ) . ';';
}
$mount_style = trim( preg_replace( '/(^|;)\s*width\s*:[^;]*/i', '$1', $style ), '; ' );
}
$player = sprintf(
'<div class="asciiplayer-container"%1$s data-ap-src="%2$s" data-ap-options="%3$s"></div>',
'' !== $mount_style ? ' style="' . esc_attr( $mount_style ) . '"' : '',
esc_url( $source, array( 'http', 'https' ) ),
esc_attr( $options )
);
if ( ! $block ) {
return $player;
}
$alignment = isset( $attributes['displayAlign'] ) && in_array( $attributes['displayAlign'], array( 'left', 'center', 'right' ), true ) ? $attributes['displayAlign'] : 'center';
$wrapper = get_block_wrapper_attributes( array( 'class' => 'asciiplayer-block' ) );
$figure = 'class="asciiplayer-figure asciiplayer-align-' . esc_attr( $alignment ) . '"' . ( '' !== $figure_style ? ' style="' . esc_attr( $figure_style ) . '"' : '' );
// RichText emits escaped text and line breaks. No other markup is needed.
$caption = isset( $attributes['caption'] ) ? wp_kses( (string) $attributes['caption'], array( 'br' => array() ) ) : '';
return '<div ' . $wrapper . '><figure ' . $figure . '>' . $player . ( '' !== trim( $caption ) ? '<figcaption class="wp-element-caption">' . $caption . '</figcaption>' : '' ) . '</figure></div>';
}
/**
* Preserve [asciiplayer]URL[/asciiplayer] and its existing options.
*
* @param mixed $attributes Shortcode attributes.
* @param string|null $content Enclosed URL.
* @return string
*/
function render_shortcode( $attributes = array(), $content = null ) {
return null === $content ? '' : render_player( $attributes, $content );
}
/**
* Dynamic block callback. Both authoring paths use exactly the same sanitizer.
*
* @param array $attributes Block attributes.
* @return string
*/
function render_block( $attributes ) {
return render_player( $attributes, null, true );
}