Skip to content
This repository was archived by the owner on Dec 18, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion commands/class-make-block-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class Make_Block_Command {
* [--theme=<active-theme>]
* : The slug of the theme to create the block in.
*
* [--scss=<true|false>]
* : Whether to generate a SCSS file for the block.
*
* ## EXAMPLES
*
* wp make-block "Creode Footer" --theme=creode
Expand All @@ -41,7 +44,13 @@ public function __invoke( $args, $optional_args ) {

try {
// Create the block.
new Make_Block( $block_label, $block_theme_slug );
new Make_Block(
$block_label,
$block_theme_slug,
array(
'scss' => $optional_args['scss'] ?? true,
)
);
} catch ( Exception $e ) {
WP_CLI::error( $e->getMessage() );
}
Expand Down
1 change: 1 addition & 0 deletions docs/.vitepress/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default defineConfig({
{ text: 'What Is It?', link: '/what-is-it' },
{ text: 'Quick Start', link: '/quick-start' },
{ text: 'Upgrading', link: '/upgrading' },
{ text: 'Changelog', link: '/changelog' },
]
},
{
Expand Down
16 changes: 16 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Changelog

## 1.10.0
### SCSS File Generation
This change introduces the capability for blocks to be generated with an SCSS file. This file is added to the `assets` directory of the block. This starts to tie in nicely with the block functionality inside the new `creode/wordpress-theme` package as block SCSS can be added as part of the block generation process automatically and recompiled with default settings. This gives a nice productivity boost for developers.

If you would like to generate a block without an SCSS file, you can pass the `--no-scss` flag to the `make-block` command.

```bash
wp make-block --theme=creode --no-scss "My Block"
```

## 1.9.0
Introduces a new composer dependency for the block plugin. This dependency removes the requirement for a `loader.php` file in the mu-plugins directory and instead copies the mu-plugin.php file into the mu-plugins directory. This solves an issue where all mu-plugins were being loaded under one single mu-plugin.

The `loader.php` file should therefore be removed from your project.
56 changes: 52 additions & 4 deletions includes/class-make-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Creode_Blocks;

use Creode_Blocks\Make_Block\Actions\{Create_New_Block_Files, Rename_Files, Replace_File_Contents, Setup_Block_Include, Setup_Scss_Include};
use Creode_Blocks\Make_Block\Actions\{Create_New_Block_Files, Rename_Files, Replace_File_Contents, Setup_Block_Include, Add_Scss_To_All_File, Recompile_Assets};
use Creode_Blocks\Make_Block\Services\Block_Details;
use Creode_Blocks\Make_Block\Services\Block_Options;
use Creode_Blocks\Make_Block\Services\Block_Replacements;

/**
Expand Down Expand Up @@ -66,28 +67,63 @@ class Make_Block {
*/
public $setup_block_include;

/**
* The class for adding the SCSS to the all file.
*
* @var Add_Scss_To_All_File
*/
public $add_scss_to_all_file;

/**
* The class for recompiling assets.
*
* @var Recompile_Assets
*/
public $recompile_assets;

/**
* Block options object.
*
* @var Block_Options
*/
public Block_Options $options;

/**
* Create the block.
*
* @param string $label The label of the block.
* @param string $theme_slug The slug of the theme.
* @param array $options The options for the block.
*/
public function __construct( string $label, ?string $theme_slug = null ) {
public function __construct( string $label, ?string $theme_slug = null, array $options = array() ) {
$this->label = $label;
$this->theme_slug = $theme_slug;

$this->options = $this->get_options( $options );

// Get the package path.
$package_path = $this->get_package_path( $this->theme_slug );

// Create a new block details and replacements classes.
$this->block_details = new Block_Details( $this->label, $package_path );
$this->block_details = new Block_Details( $this->label, $package_path );
$this->block_replacements = new Block_Replacements( $this->block_details );

// Call functionality to create the new block files.
$this->create_new_block_files = new Create_New_Block_Files( $this->block_details );
$this->create_new_block_files = new Create_New_Block_Files( $this->block_details, $this->options );
$this->rename_files = new Rename_Files( $this->block_details, $this->block_replacements );
$this->replace_file_contents = new Replace_File_Contents( $this->block_details, $this->block_replacements );
$this->setup_block_include = new Setup_Block_Include( $this->block_details, $this->block_replacements );
$this->add_scss_to_all_file = new Add_Scss_To_All_File( $this->get_theme_slug() );
$this->recompile_assets = new Recompile_Assets( $this->get_theme_slug() );
}

/**
* Get the theme slug.
*
* @return string
*/
protected function get_theme_slug() {
return $this->theme_slug ?? get_stylesheet();
}

/**
Expand All @@ -109,4 +145,16 @@ protected function get_package_path( ?string $theme_slug ) {

return $theme_base_path . '/blocks';
}

/**
* Parse the options.
*
* @param array $options The options for the block.
*
* @return Block_Options
*/
protected function get_options( array $options ): Block_Options {
return (new Block_Options( $options ))
->set_scss( $options['scss'] ?? true );
}
}
35 changes: 35 additions & 0 deletions includes/make-block/actions/class-add-scss-to-all-file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Creode_Blocks\Make_Block\Actions;

use Creode_Theme\All_Blocks_Scss_File_Generator;
use Creode_Theme\Command_Message_Handler;

/**
* Handles the addition of SCSS to the all file.
*/
class Add_Scss_To_All_File {
/**
* Constructor for class.
*
* @param string $theme_slug The slug of the theme.
*/
public function __construct( protected string $theme_slug ) {
$this->handle();
}

/**
* Delegates the SCSS generation to the theme modifier.
*
* @throws Exception If the block already exists.
*/
protected function handle() {
// Check if a class exists in theme.
if ( ! class_exists( 'Creode_Theme\All_Blocks_Scss_File_Generator' ) ) {
return;
}

// Create a new instance of the All Blocks SCSS File Generator.
new All_Blocks_Scss_File_Generator( $this->theme_slug, new Command_Message_Handler() );
}
}
41 changes: 28 additions & 13 deletions includes/make-block/actions/class-create-new-block-files.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,26 @@
namespace Creode_Blocks\Make_Block\Actions;

use Creode_Blocks\Make_Block\Services\Block_Details;
use Creode_Blocks\Make_Block\Services\Block_Options;

/**
* Handles the creation of new block files.
*/
class Create_New_Block_Files {
/**
* The class for holding the block details.
* Constructor for the class.
*
* @var Block_Details
* @param Block_Details $block_details The block details.
* @param boolean $no_scss The no scss flag.
*/
protected $block_details;

public function __construct( Block_Details $block_details ) {
$this->block_details = $block_details;

public function __construct( protected Block_Details $block_details, protected Block_Options $block_options ) {
$this->handle();
}

/**
* Runs the creation of the new block files.
*
* @throws Exception If the block already exists.
* @throws \Exception If the block already exists.
*/
protected function handle() {
// Get the block folder path.
Expand All @@ -37,15 +35,16 @@ protected function handle() {

// Create the block folder, if it doesn't exist.
if ( ! file_exists( $block_folder_path ) ) {
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_mkdir
mkdir( $block_folder_path );
}

// Copy the stubs directory to the block folder.
$source = $this->block_details->get_stubs_path() . '/block';
$source = $this->block_details->get_stubs_path() . '/block';
$destination = $block_folder_path;

// Recursively copy the stubs directory to the block folder.
self::recursive_copy( $source, $destination );
self::recursive_copy( $source, $destination, $this->block_options );
}

/**
Expand All @@ -55,20 +54,36 @@ protected function handle() {
* @param string $destination The destination directory.
* @return void
*/
protected static function recursive_copy( $source, $destination ) {
protected static function recursive_copy( $source, $destination, Block_Options $block_options ) {
$dir = opendir( $source );
@mkdir( $destination );

while ( false !== ( $file = readdir( $dir ) ) ) {
if ( '.' !== $file && '..' !== $file ) {
if ( is_dir( $source . '/' . $file ) ) {
self::recursive_copy( $source . '/' . $file, $destination . '/' . $file );
self::recursive_copy( $source . '/' . $file, $destination . '/' . $file, $block_options );
} else {
copy( $source . '/' . $file, $destination . '/' . $file );
self::copy_file( $source . '/' . $file, $destination . '/' . $file, $block_options );
}
}
}

closedir( $dir );
}

/**
* Copy a file to the destination.
*
* @param string $source The source file.
* @param string $destination The destination file.
* @return void
*/
protected static function copy_file( $source, $destination, Block_Options $block_options ) {
// If the file is a scss file and the no scss flag is set, skip the file.
if ( str_ends_with( $source, '.scss' ) && ! $block_options->get_scss() ) {
return;
}

copy( $source, $destination );
}
}
37 changes: 37 additions & 0 deletions includes/make-block/actions/class-recompile-assets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Handles the recompilation of assets.
*
* @package Creode Blocks
*/

namespace Creode_Blocks\Make_Block\Actions;

use Creode_Theme\Asset_Builder;
use Creode_Theme\Command_Message_Handler;

/**
* Handles the recompilation of assets.
*/
class Recompile_Assets {
/**
* Constructor for class.
*
* @param string $theme_slug The slug of the theme.
*/
public function __construct( protected string $theme_slug ) {
$this->handle();
}

/**
* Delegates the asset recompilation to the theme modifier.
*/
protected function handle() {
// Check if a class exists in theme.
if ( ! class_exists( 'Creode_Theme\Asset_Builder' ) ) {
return;
}

new Asset_Builder( $this->theme_slug, new Command_Message_Handler() );
}
}
2 changes: 1 addition & 1 deletion includes/make-block/actions/class-rename-files.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Rename_Files {
* @param Block_Replacements $block_replacements
*/
public function __construct( Block_Details $block_details, Block_Replacements $block_replacements ) {
$this->block_details = $block_details;
$this->block_details = $block_details;
$this->block_replacements = $block_replacements;

$this->handle();
Expand Down
44 changes: 44 additions & 0 deletions includes/make-block/services/class-block-options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Creode_Blocks\Make_Block\Services;

/**
* Handles the block options.
*/
class Block_Options {
/**
* The no scss flag.
*
* @var bool
*/
protected bool $scss = true;

/**
* Constructor.
*/
public function __construct() {}

/**
* Set the scss flag.
*
* @param boolean $scss The scss flag.
*
* @return boolean
*/
public function get_scss(): bool {
return $this->scss;
}

/**
* Set the scss flag.
*
* @param boolean $scss The scss flag.
*
* @return static
*/
public function set_scss( bool $scss ): static {
$this->scss = $scss;

return $this;
}
}
26 changes: 26 additions & 0 deletions includes/make-block/stubs/block/assets/_:BLOCK_SLUG.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@use '/scss/globals';
@use '/scss/extenders/container';

@mixin render {
.:BLOCK_SLUG__outer-wrapper,
.wp-block-acf-:BLOCK_SLUG {
background-color: white;
color: black;

& * {
color: inherit;
}
}

.:BLOCK_SLUG__outer-wrapper {
@extend %container__outer-wrapper;
}

.:BLOCK_SLUG__wrapper {
@extend %container__wrapper;
}

.:BLOCK_SLUG__inner {
@extend %container__inner;
}
}
Loading