diff --git a/inc/generateBlock.js b/inc/generateBlock.js new file mode 100644 index 0000000..314f4e4 --- /dev/null +++ b/inc/generateBlock.js @@ -0,0 +1,266 @@ +/** + * Génération automatisée de Blocks WordPress sur le modèle de Beflex + * Ligne de génération : node inc/generateBlock.js inc/blocks block +*/ + +const fs = require('fs'); +const path = require('path'); + +function generatePHPFile(folder, fileName, content, keywords) { + let modifiedContent = content; + + // Remplacer les mots-clés + Object.keys(keywords).forEach((keyword) => { + const regex = new RegExp(`{{${keyword}}}`, 'g'); + modifiedContent = modifiedContent.replace(regex, keywords[keyword]); + }); + + const filePath = path.join(folder, fileName); + fs.writeFileSync(filePath, modifiedContent, 'utf-8'); + console.log(`Fichier ${filePath} généré avec succès.`); +} + +function generateVariants(value) { + const lowerCase = value.toLowerCase(); + const snakeCase = value.replace(/-/g, '_'); + const upperCase = snakeCase.toUpperCase(); + + // Ajouter le préfixe "bf-" + const bfLowerCase = `bf-${lowerCase}`; + const bfUpperCase = `BF_${upperCase}`; + const bfSnakeCase = `bf_${snakeCase}`; + + return { + blockShort: value, + blockName: bfLowerCase, + blockVar: bfUpperCase, + blockFunc: bfSnakeCase + // blockNameUpperCase: upperCase, + // blockNameSnakeCase: snakeCase, + // bfBlockName: bfLowerCase, + // bfBlockNameUpperCase: bfUpperCase, + // bfBlockNameSnakeCase: bfSnakeCase + }; + + // 'blockShort': blockShort, +// 'blockName': blockName, +// 'blockVar': blockVar, +// 'blockFunc': blockFunc +} + +function generateSCSSFile(folder, keywords) { + const scssContent = `/** + * Style CSS de {{blockName}} +*/`; + generatePHPFile(path.join(folder, 'assets/scss'), 'style.scss', scssContent, keywords); +} + +function generateJSFiles(folder, keywords) { + const jsContent = `(function($){ + /** + * initializeBlock + * + * Adds custom JavaScript to the block HTML. + * + * @param object $block The block jQuery element. + * @param object attributes The block attributes (only available when editing). + * @return void + */ + var initializeBlock = function( $block ) { + } + + // Initialize each block on page load (front end). + $(document).ready(function(){ + $('.{{blockName}}').each(function(){ + initializeBlock( $(this) ); + }); + }); + + // Initialize dynamic block preview (editor). + if( window.acf ) { + window.acf.addAction( 'render_block_preview/type={{blockName}}', initializeBlock ); + } + +})(jQuery); + `; + const jsFileName = `${keywords.blockName}.js`; // Utilisation du mot-clé blockName + generatePHPFile(path.join(folder, 'assets/js'), jsFileName, jsContent, keywords); +} + +function generateJsonFile(folder, keywords) { + const jsonContent = `{ + "$schema": "https://schemas.wp.org/trunk/block.json", + "name": "beflex/{{blockShort}}", + "title": "{{blockShort}}", + "description": "", + "style": [ "block-{{blockName}}-style" ], + "viewScript": [ "block-{{blockName}}-script" ], + "category": "beflex", + "icon": "", + "apiVersion": 2, + "textdomain": "beflex", + "keywords": [ + "beflex", + "{{blockShort}}" + ], + "supports": { + }, + "acf": { + "mode": "auto", + "renderTemplate": "../../view.php", + "postTypes": [ + "post", + "page" + ] + } +} + `; + generatePHPFile(path.join(folder, 'assets/json'), 'block.json', jsonContent, keywords); +} + +function generatePHPFiles(folder, keywords) { + const phpContent = ` + * @link https://developer.wordpress.org/themes/basics/theme-functions/ + * @since 4.0.0 + * @package beflex-child + * + */ + +if ( ! defined( '{{blockVar}}_DIR' ) ) { + define( '{{blockVar}}_DIR', dirname(__DIR__, 1) . '/{{blockName}}' ); +} +if ( ! defined( '{{blockVar}}_URL' ) ) { + define( '{{blockVar}}_URL', get_stylesheet_directory_uri() . '/inc/blocks/{{blockName}}' ); +} + +/** + * Generate block + * + * @return void + */ +function {{blockFunc}}_register_acf_blocks() { + wp_register_style( 'block-{{blockName}}-style', {{blockVar}}_URL . '/assets/css/style.min.css' ); + wp_register_script( 'block-{{blockName}}-script', {{blockVar}}_URL . '/assets/js/{{blockName}}.js', array('jquery'), '', true ); + register_block_type( {{blockVar}}_DIR . '/assets/json/' ); +} +add_action( 'init', '{{blockFunc}}_register_acf_blocks', 5 ); + +/** + * Load Json fields + * + * @param array $paths Json path + * + * @return array $paths Json path + */ +function {{blockFunc}}_load_json( $paths ) { + $paths[] = {{blockVar}}_DIR . '/assets/json'; + return $paths; +} +add_filter( 'acf/settings/load_json', '{{blockFunc}}_load_json' ); + `; + const phpFileName = `${keywords.blockName}.php`; // Uti + generatePHPFile(folder, phpFileName, phpContent, keywords); + + const phpContentView = ` + +