153 lines
4.6 KiB
PHP
153 lines
4.6 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');
|
|
|
|
|
|
|
|
/* ############## DISALLOW BLOCKS ############## */
|
|
|
|
/**
|
|
* Désactive le bloc audio natif pour les utilisateurs non administrateurs pour éviter toute confusio
|
|
*
|
|
*/
|
|
function carhop_blocks_disallow_block_types($allowed_block_types, $block_editor_context)
|
|
{
|
|
|
|
|
|
|
|
$disallowed_blocks = array(
|
|
'core/audio',
|
|
);
|
|
|
|
// Get all registered blocks if $allowed_block_types is not already set.
|
|
if (! is_array($allowed_block_types) || empty($allowed_block_types)) {
|
|
$registered_blocks = WP_Block_Type_Registry::get_instance()->get_all_registered();
|
|
$allowed_block_types = array_keys($registered_blocks);
|
|
}
|
|
|
|
// Create a new array for the allowed blocks.
|
|
$filtered_blocks = array();
|
|
|
|
// Loop through each block in the allowed blocks list.
|
|
foreach ($allowed_block_types as $block) {
|
|
|
|
// Check if the block is not in the disallowed blocks list.
|
|
if (! in_array($block, $disallowed_blocks, true)) {
|
|
|
|
// If it's not disallowed, add it to the filtered list.
|
|
$filtered_blocks[] = $block;
|
|
}
|
|
}
|
|
|
|
|
|
// Return the filtered list of allowed blocks
|
|
return $filtered_blocks;
|
|
}
|
|
add_filter('allowed_block_types_all', 'carhop_blocks_disallow_block_types', 10, 2);
|