<?php

/**
 * Plugin Name:       Homegrade Blocks
 * Description:       Un série de Blocks sur mesure pour l'écriture du contenu Homegrade
 * Author:            Deligraph
 * Text Domain:       homegrade-blocks
 * Version:           1.0.0
 */

/* ---------------------------
	BLOCK CATEGORY
 ---------------------------*/
function homegrade_add_block_categories($categories)
{
	return array_merge(

		[
			[
				'slug'  => 'homegrade-blocks',
				'title' => __('Blocs Homegrade ', 'homegrade-blocks'),
				'icon'  => 'heart',

			],
			[
				'slug'  => 'homegrade-pages',
				'title' => 'Homegrade Specific Page Blocks ',
				'icon'  => 'admin-page',
			],
			[
				'slug'  => 'homegrade-page-home',
				'title' => 'Homegrade Blocks | Page d\'accueil',
				'icon'  => 'admin-page',
			],
		],
		$categories
	);
}
add_action('block_categories_all', 'homegrade_add_block_categories', 25, 2);


/* ---------------------------
	 REGISTER BLOCKS
 ---------------------------*/
function create_block_multiblocks_block_init()
{
	wp_register_script(
		'internationnalisation-script',
		plugins_url('/blocks/internationnalisation/build/index.js', __FILE__),
		array('wp-i18n'),
	);


	// Dynamics 
	register_block_type(__DIR__ . '/blocks/questions-container/build');
	register_block_type(__DIR__ . '/blocks/post-content-container/build');
	register_block_type(__DIR__ . '/blocks/vocabulaire-summary/build');
	register_block_type(__DIR__ . '/blocks/content-page-header/build');
	register_block_type(__DIR__ . '/blocks/communiques-presse-feed/build');
	register_block_type(__DIR__ . '/blocks/kit-presse/build');
	register_block_type(__DIR__ . '/blocks/press-photo-kit/build');
	register_block_type(__DIR__ . '/blocks/press-reviews/build');
	register_block_type(__DIR__ . '/blocks/offres-emplois/build');
	register_block_type(__DIR__ . '/blocks/client-statisfaction/build');
	register_block_type(__DIR__ . '/blocks/post-card/build');
	register_block_type(__DIR__ . '/blocks/page-card/build');
	// register_block_type(__DIR__ . '/blocks/internationnalisation/build');

	// Statics
	register_block_type(__DIR__ . '/blocks/focused-thematique/build');
	register_block_type(__DIR__ . '/blocks/focused-schema/build');
	register_block_type(__DIR__ . '/blocks/points-cles/build');
	register_block_type(__DIR__ . '/blocks/content-heading/build');
	register_block_type(__DIR__ . '/blocks/picture-banner/build');
	register_block_type(__DIR__ . '/blocks/highlight/build');
	register_block_type(__DIR__ . '/blocks/aside/build');
	register_block_type(__DIR__ . '/blocks/plus-loin/build');
	register_block_type(__DIR__ . '/blocks/chapitrage-thematique/build');
	register_block_type(__DIR__ . '/blocks/timeline/build');
	register_block_type(__DIR__ . '/blocks/staff-list/build');
	register_block_type(__DIR__ . '/blocks/staff-member/build');
	register_block_type(__DIR__ . '/blocks/section-titling/build');
	register_block_type(__DIR__ . '/blocks/accordeon/build');
	register_block_type(__DIR__ . '/blocks/column-cards/build');
	register_block_type(__DIR__ . '/blocks/card-free/build');
	register_block_type(__DIR__ . '/blocks/text-image/build');
	register_block_type(__DIR__ . '/blocks/icon/build');
	register_block_type(__DIR__ . '/blocks/tips-to-know/build');
	register_block_type(__DIR__ . '/blocks/labelled-picture/build');
	register_block_type(__DIR__ . '/blocks/partner-logo/build');
	register_block_type(__DIR__ . '/blocks/infos-seminaires/build');
	register_block_type(__DIR__ . '/blocks/help-homegrade/build');
	register_block_type(__DIR__ . '/blocks/partners-other-services/build');
	register_block_type(__DIR__ . '/blocks/date/build');
	register_block_type(__DIR__ . '/blocks/shortcuts/build');
	register_block_type(__DIR__ . '/blocks/parcours-renovateur/build');
	register_block_type(__DIR__ . '/blocks/parcours-chapter/build');
	register_block_type(__DIR__ . '/blocks/parcours-step/build');
	register_block_type(__DIR__ . '/blocks/figure/build');
	register_block_type(__DIR__ . '/blocks/explainer-screen/build');
	register_block_type(__DIR__ . '/blocks/cta-slim/build');

	// CONTAINERS (STATIC)
	register_block_type(__DIR__ . '/blocks/content-box/build');

	// CHILDREN (STATIC)
	register_block_type(__DIR__ . '/blocks/monoblock/build');  // — ALLOWED IN GREYBOX / WHITE BOX

	// ACF
	register_block_type(__DIR__ . '/acf-blocks/google-map');
	register_block_type(__DIR__ . '/acf-blocks/lightbox-gallery');
	register_block_type(__DIR__ . '/acf-blocks/sitemap');
	register_block_type(__DIR__ . '/acf-blocks/photos-archives');
	register_block_type(__DIR__ . '/acf-blocks/dates-seminaire');
	register_block_type(__DIR__ . '/acf-blocks/home/home-header');
	register_block_type(__DIR__ . '/acf-blocks/home/latest-news');
	register_block_type(__DIR__ . '/acf-blocks/home/show-thematiques');
	register_block_type(__DIR__ . '/acf-blocks/home/aides-financieres');
	register_block_type(__DIR__ . '/acf-blocks/home/demarches-administratives');
	register_block_type(__DIR__ . '/acf-blocks/home/questions-frequentes');
}
add_action('init', 'create_block_multiblocks_block_init');



//##### SET SCRIPT TRANSLATIONS
function set_translation_script()
{
	wp_set_script_translations("homegrade-content-blocks-communiques-presse-feed-editor-script", 'homegrade-blocks', plugin_dir_path(__FILE__) . 'languages');
}
add_action('init', 'set_translation_script');


/* ---------------------------
	ENQUEUE EDITOR JS CONTEXT
 ---------------------------*/
function homegrade_blocks_enqueue_editor_assets()
{

	$asset_file = include(plugin_dir_path(__FILE__) . 'build/index.asset.php');
	wp_enqueue_script('homegrade-blocks-script', plugins_url('build/index.js', __FILE__), $asset_file['dependencies'], $asset_file['version']);
	wp_enqueue_style('blocks-course-plugin-boilerplate-style', plugins_url('build/index.css', __FILE__));
}

add_action('enqueue_block_editor_assets', 'homegrade_blocks_enqueue_editor_assets');



/* -------------------------------------
	ENQUEUE SCRIPTS AND STYLES FOR FRONT
 -------------------------------------*/
function homegrade_blocks_plugin_enqueue_assets()
{

	//Enqueue script onlye if it is the main site of the network
	if (is_main_site()) {

		$asset_file = include(plugin_dir_path(__FILE__) . 'build/index.asset.php');
		wp_enqueue_script('homegrade-blocks-tooltipjs', plugin_dir_url(__FILE__) . 'build/tooltipFront.js');
		wp_enqueue_style('homegrade-blocks-tooltipcss', plugin_dir_url(__FILE__) . 'src/format-types/tooltip/tooltip.css');
		wp_enqueue_script('homegrade-blocks-brochurejs', plugin_dir_url(__FILE__) . 'src/format-types/brochure/brochure-front.js');
	}
	wp_enqueue_style('homegrade-blocks-boulecss', plugin_dir_url(__FILE__) . 'src/format-types/boule/boule.css');

	// #PASSING TRANSLATIONS
	wp_register_script('homegrade-blocks-brochurejs', plugin_dir_url(__FILE__) . 'src/format-types/brochure/brochure-front.js');
	$translation_array_brochure_link = array(
		'download' => __('Télécharger', 'homegrade-blocks'),
	);
	wp_localize_script('homegrade-blocks-brochurejs', 'textTranslationsBrochureLink', $translation_array_brochure_link);
	wp_localize_script('homegrade-blocks-brochurejs', 'img_path_datas', array(
		'downloadIconPath' => plugin_dir_url(__FILE__) . '/src/img/icon_brochure_download.svg'
	));
	wp_enqueue_script('homegrade-blocks-brochurejs');



	wp_enqueue_style('homegrade-blocks-brochurecss', plugin_dir_url(__FILE__) . 'src/format-types/brochure/brochure.css');
	$theme = wp_get_theme();

	// SHOW THEMATIQUE 
	wp_register_script('showthematiquejs', plugin_dir_url(__FILE__) . '/acf-blocks/home/show-thematiques/show-thematiques.js', array('swiperjs'), $theme->get('Version'));
	$translation_array = array(
		'previousSlide' => __('Thématique précédente', 'homegrade-theme__texte-fonctionnel'),
		'nextSlide' => __('Thématique suivante', 'homegrade-theme__texte-fonctionnel'),
		'showThematiques' => __('Afficher les thématiques', 'homegrade-theme__texte-fonctionnel'),
		'to' => __('à', 'homegrade-theme__texte-fonctionnel'),
		'slideLabel' => __('Carte Thématique', 'homegrade-theme__texte-fonctionnel'),
	);
	wp_localize_script('showthematiquejs', 'textTranslations', $translation_array);
	wp_enqueue_script('showthematiquejs');
	// write_log(plugin_dir_path(__FILE__) . '/acf-blocks/home/show-thematiques/show-thematiques.js');
	// write_log(__DIR__ . '/acf-blocks/home/show-thematiques/show-thematiques.js');
}
add_action('wp_enqueue_scripts', 'homegrade_blocks_plugin_enqueue_assets');


function add_type_attribute($tag, $handle, $src)
{
	// if not your script, do nothing and return original $tag
	if ('homegrade-blocks-tooltipjs' !== $handle) {
		return $tag;
	}
	// change the script tag by adding type="module" and return it.
	$tag = '<script type="module" src="' . esc_url($src) . '"></script>';
	return $tag;
}
add_filter('script_loader_tag', 'add_type_attribute', 10, 3);

/* ---------------------------
	ALLOWED BLOCKS
 ---------------------------*/

function homegrade_allowed_block_types($allowed_blocks, $editor_context)
{
	if ('conseils' === $editor_context->post->post_type) {
		$allowed_blocks = array(
			'core/paragraph',
			'core/list',
			'core/list-item',
			'homegrade-content-blocks/chapter-header',
			'homegrade-content-blocks/points-cles',
			'homegrade-content-blocks/questions-container'
		);
	}


	if ('fiche-questions' === $editor_context->post->post_type) {
		$allowed_blocks = array(
			'core/paragraph',
			'core/heading',
			'core/list',
			'core/list-item',
			'homegrade-content-blocks/chapter-header',
			'homegrade-content-blocks/points-cles',
			'homegrade-content-blocks/questions-container'
		);
	}
	return $allowed_blocks;

	// $blocks = WP_Block_Type_Registry::get_instance()->get_all_registered();
	// write_log($blocks);
	####ONLY DEREGISTER FEW BLOCKS
	// get all the registered blocks
	// $blocks = WP_Block_Type_Registry::get_instance()->get_all_registered();
	// write_log($blocks);
	// then disable some of them
	// unset($blocks['core/paragraph']);
	// unset($blocks['core/heading']);

	// return the new list of allowed blocks
	// return array_keys($blocks);
}

// add_filter('homegrade_allowed_block_types', 'homegrade_allowed_block_types', 25, 2);



function homegrade_allowed_block_types_all($block_editor_context, $editor_context)
{
	if ('conseils' === $editor_context->post->post_type) {
		// write_log($editor_context);
	}
	if (!empty($editor_context->post)) {
		return array(
			'core/paragraph',
			'core/heading',
			'core/list',
		);
	}

	return $block_editor_context;
}

// add_filter('allowed_block_types_all', 'homegrade_allowed_block_types_all', 10, 2);
/* -------------------------------------
	POST TYPE TEMPLATES
 -------------------------------------*/
include_once('templates.php');




/* -------------------------------------
	CORE BLOCKS ADJUSTMENTS
 -------------------------------------*/
function enqueue_block_editor_adjustments()
{


	wp_register_script(
		'block_editor_adjustments',
		plugin_dir_url(__FILE__) . '/build/editorAdjustements.js',
		['wp-blocks', 'wp-dom', 'wp-dom-ready', 'wp-edit-post'],
		filemtime(plugin_dir_path(__FILE__) . '/build/editorAdjustements.js')

	);

	wp_enqueue_script('block_editor_adjustments');
}


add_action('enqueue_block_editor_assets', 'enqueue_block_editor_adjustments');


// ###### PHP INTERNATIONNALISATION 

add_action('init', 'wpdocs_load_textdomain');

function wpdocs_load_textdomain()
{
	load_plugin_textdomain('homegrade-blocks', false, dirname(plugin_basename(__FILE__)) . '/languages');
}


require_once(__DIR__ . '/blocks/highlight/highlight.php');
require_once(__DIR__ . '/acf-blocks/google-map/map-api-key.php');
