carhop__plugins__PROD-DEV/plugins/carhop-blocks/blocks/audio-player/audio-player.php
Antoine M d62e3ca3b9
All checks were successful
continuous-integration/drone/push Build is passing
UPDATE Update audio player block to use local Plyr assets and add build scripts for asset management
2025-10-29 17:20:39 +01:00

110 lines
3.5 KiB
PHP

<?php
/**
* Plugin Name: Audio Player
* Description: Example block scaffolded with Create Block tool.
* Version: 0.1.0
* Requires at least: 6.7
* Requires PHP: 7.4
* Author: The WordPress Contributors
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: audio-player
*
* @package CreateBlock
*/
if (! defined('ABSPATH')) {
exit; // Exit if accessed directly.
}
/**
* Register Plyr library assets
* Assets are copied from node_modules to assets/plyr for production deployment
*/
function create_block_audio_player_register_plyr()
{
// Register Plyr CSS
wp_register_style(
'plyr',
plugins_url('assets/plyr/plyr.css', __FILE__),
array(),
'3.8.3'
);
// Register Plyr JS
wp_register_script(
'plyr',
plugins_url('assets/plyr/plyr.min.js', __FILE__),
array(),
'3.8.3',
true
);
}
add_action('init', 'create_block_audio_player_register_plyr');
/**
* Enqueue Plyr in the block editor
*/
function create_block_audio_player_enqueue_editor_assets()
{
wp_enqueue_style('plyr');
wp_enqueue_script('plyr');
}
add_action('enqueue_block_editor_assets', 'create_block_audio_player_enqueue_editor_assets');
/**
* Enqueue Plyr assets when the block is used
*/
function create_block_audio_player_enqueue_assets($block_content, $block)
{
if ('carhop-blocks/audio-player' === $block['blockName']) {
wp_enqueue_style('plyr');
wp_enqueue_script('plyr');
}
return $block_content;
}
add_filter('render_block', 'create_block_audio_player_enqueue_assets', 10, 2);
/**
* Registers the block using a `blocks-manifest.php` file, which improves the performance of block type registration.
* Behind the scenes, it also registers all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://make.wordpress.org/core/2025/03/13/more-efficient-block-type-registration-in-6-8/
* @see https://make.wordpress.org/core/2024/10/17/new-block-type-registration-apis-to-improve-performance-in-wordpress-6-7/
*/
function create_block_audio_player_block_init()
{
/**
* Registers the block(s) metadata from the `blocks-manifest.php` and registers the block type(s)
* based on the registered block metadata.
* Added in WordPress 6.8 to simplify the block metadata registration process added in WordPress 6.7.
*
* @see https://make.wordpress.org/core/2025/03/13/more-efficient-block-type-registration-in-6-8/
*/
if (function_exists('wp_register_block_types_from_metadata_collection')) {
wp_register_block_types_from_metadata_collection(__DIR__ . '/build', __DIR__ . '/build/blocks-manifest.php');
return;
}
/**
* Registers the block(s) metadata from the `blocks-manifest.php` file.
* Added to WordPress 6.7 to improve the performance of block type registration.
*
* @see https://make.wordpress.org/core/2024/10/17/new-block-type-registration-apis-to-improve-performance-in-wordpress-6-7/
*/
if (function_exists('wp_register_block_metadata_collection')) {
wp_register_block_metadata_collection(__DIR__ . '/build', __DIR__ . '/build/blocks-manifest.php');
}
/**
* Registers the block type(s) in the `blocks-manifest.php` file.
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
$manifest_data = require __DIR__ . '/build/blocks-manifest.php';
foreach (array_keys($manifest_data) as $block_type) {
register_block_type(__DIR__ . "/build/{$block_type}");
}
}
add_action('init', 'create_block_audio_player_block_init');