velora-player/includes/class-shortcode.php
2026-03-23 16:55:03 +01:00

99 lines
2.1 KiB
PHP

<?php
/**
* Shortcode registration.
*
* @package ModernAudioPlayer
*/
namespace ModernAudioPlayer;
defined( 'ABSPATH' ) || exit;
class Shortcode {
/**
* Register shortcode hooks.
*
* @return void
*/
public function register() {
add_shortcode( 'audio_player', array( $this, 'render' ) );
}
/**
* Render the shortcode.
*
* @param array<string, mixed> $atts Shortcode attributes.
* @return string
*/
public function render( $atts ) {
$atts = shortcode_atts(
array(
'src' => '',
'title' => '',
'image' => '',
'playlist' => '',
'theme' => '',
),
(array) $atts,
'audio_player'
);
$settings = Settings::get();
$attrs = array(
'src' => $atts['src'],
'title' => $atts['title'],
'image' => $atts['image'],
'playlist' => $this->parse_playlist_attribute( $atts['playlist'] ),
'theme' => $atts['theme'] ? $atts['theme'] : $settings['default_theme'],
'useGlobalTheme' => empty( $atts['theme'] ),
);
return Renderer::render_player( $attrs );
}
/**
* Parse playlist attribute into normalized track items.
*
* Supported formats:
* - JSON array of objects with src/title/image
* - Line-based entries: title|src|image
*
* @param string $playlist_raw Raw playlist value.
* @return array<int, array<string, string>>
*/
private function parse_playlist_attribute( $playlist_raw ) {
$playlist_raw = trim( html_entity_decode( (string) $playlist_raw, ENT_QUOTES, get_bloginfo( 'charset' ) ) );
if ( '' === $playlist_raw ) {
return array();
}
$decoded = json_decode( $playlist_raw, true );
if ( is_array( $decoded ) ) {
return $decoded;
}
$tracks = array();
$lines = preg_split( '/\r\n|\r|\n/', $playlist_raw );
foreach ( (array) $lines as $line ) {
$line = trim( (string) $line );
if ( '' === $line ) {
continue;
}
$parts = array_map( 'trim', explode( '|', $line ) );
$tracks[] = array(
'title' => $parts[0] ?? '',
'src' => $parts[1] ?? '',
'image' => $parts[2] ?? '',
);
}
return $tracks;
}
}