<?php

/**
 * Plugin Name:       Carhop Blocks
 * Description:       Un série de Blocks sur mesure pour l'écriture du contenu de Carhop
 * Author:            Deligraph
 * Text Domain:       carhop-blocks
 * Version:           0.1.0
 * Requires at least: 6.7
 * Requires PHP:      7.4
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       carhop-blocks
 * 
 *
 * @package CreateBlock
 */

if (! defined('ABSPATH')) {
	exit; // Exit if accessed directly.
}
##### REGISTER BLOCKS #####
function create_block_carhop_blocks_block_init()
{
	// ##### REGISTER ACF BLOCKS #####
	register_block_type(__DIR__ . '/acf-blocks/gallery');
	register_block_type(__DIR__ . '/acf-blocks/social-networks');
	register_block_type(__DIR__ . '/acf-blocks/custom-video');
	register_block_type(__DIR__ . '/acf-blocks/custom-audio');
	register_block_type(__DIR__ . '/acf-blocks/team-carhop');
	register_block_type(__DIR__ . '/acf-blocks/member-card');
	register_block_type(__DIR__ . '/acf-blocks/statistiques-collections');

	$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_carhop_blocks_block_init');

##### REGISTER BLOCK CATEGORIES #####
function carhop_add_block_categories($categories)
{
	return array_merge(

		[
			[
				'slug'  => 'carhop-blocks',
				'title' => __('Blocs Carhop ', 'carhop-blocks'),
				'icon'  => 'heart',
			],

		],
		$categories
	);
}
add_action('block_categories_all', 'carhop_add_block_categories', 25, 2);

##### CUSTOM BLOCKS SETTINGS #####
require_once __DIR__ . '/src/audio-player/audio-player.php';

/* ----------------------------------------------------------------
	##### ENQUEUE BLOCKS
 ------------------------------------------------------------------*/

function carhop_register_blocks()
{
	if (!function_exists('register_block_type')) {
		return;
	}

	// Enregistrer le script pour le bloc gallery
	wp_register_script(
		'carhop-gallery-view',
		plugins_url('acf-blocks/gallery/view.js', __FILE__),
		array(),
		filemtime(__DIR__ . '/acf-blocks/gallery/view.js'),
		true
	);

	wp_enqueue_style(
		'carhop-blocks-variations',
		plugins_url('build/variations.css', __FILE__),

	);
}
add_action('init', 'carhop_register_blocks');


/* ----------------------------------------------------------------
	##### EDITOR EXTENSIONS
 ------------------------------------------------------------------*/
/**
 * Enqueue plusieurs extensions éditeur (variantes/styles pour blocs core).
 */
function carhop_enqueue_editor_assets()
{
	$asset_file = include __DIR__ . '/build/variations.asset.php';
	wp_enqueue_script(
		'carhop-variants',
		plugins_url('build/variations.js', __FILE__),
	);
}
add_action('enqueue_block_editor_assets', 'carhop_enqueue_editor_assets');


function carhop_enqueue_variants_admin_scripts()
{
	$css = __DIR__ . '/build/variations.css';
	if (file_exists($css)) {
		wp_enqueue_style(
			'carhop-variants',
			plugins_url('build/variations.css', __FILE__),
			array(),
			filemtime($css)
		);
	}
}
add_action('admin_enqueue_scripts', 'carhop_enqueue_admin_scripts');
