Skip to content

Commit

Permalink
Merge branch 'release/1.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubmikita committed Sep 7, 2020
2 parents 3422cff + 1b7fc5d commit 730ec0a
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 13 deletions.
18 changes: 17 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
# Changelog
All notable changes to this project will be documented in this file.

## [1.0.0] - 27.01.2020
## 1.0.3

* [Fixed] jQuery compatibility in WordPress 5.5.
* [Fixed] ACF 5.9 compatibility.
* [Fixed] Block creation when using layout fields like Message or Tabs.
* [Changed] Block template.
* [Added] Support for line indentation.

## 1.0.2

* [Added] InnerBlocks support.

## 1.0.1

* [Added] stylelint-disable comment in scss file.

## 1.0.0

Initial release
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
[![Total Downloads](https://poser.pugx.org/micropackage/acf-block-creator/downloads)](https://packagist.org/packages/micropackage/acf-block-creator)
[![License](https://poser.pugx.org/micropackage/acf-block-creator/license)](https://packagist.org/packages/micropackage/acf-block-creator)

<p align="center">
<img src="https://bracketspace.com/extras/micropackage/micropackage-small.png" alt="Micropackage logo"/>
</p>

## 🧬 About ACF Block Creator

This package simplifies block creation for Gutengberg editor in WordPress using Advanced Custom Fields plugin.
Expand Down
6 changes: 3 additions & 3 deletions assets/js/acf-block-creator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
( function( $ ) {

var $block_option_fields = $( '#acf-field-group-options .acf-fields' ).find( '[data-name="block_name"], [data-name="block_slug"], [data-name="block_category"], [data-name="block_align"], [data-name="block_container_class"]' );
var $block_option_fields = $( '#acf-field-group-options .acf-fields' ).find( '[data-name="block_name"], [data-name="block_slug"], [data-name="block_category"], [data-name="block_align"], [data-name="block_container_class"], [data-name="inner_blocks"]' );

$block_option_fields.hide();

Expand All @@ -14,8 +14,8 @@
}
} );

$( '#acf_field_group-create_gutenberg_block' ).click( function() {
if ( $( this ).attr( 'checked' ) ) {
$( '#acf_field_group-create_gutenberg_block' ).change( function() {
if ( $( this ).is( ':checked' ) ) {
$block_option_fields.show();
} else {
$block_option_fields.hide();
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "micropackage/acf-block-creator",
"version": "1.0.0",
"version": "1.0.3",
"description": "ACF Block Creator - automatic block creation add-on for ACF",
"license": "GPL-3.0-or-later",
"authors": [
Expand Down
72 changes: 65 additions & 7 deletions src/ACFBlockCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ public static function init( $config = [] ) {
*/
private $config;

/**
* Field types excluded from markup generation
*
* @var array
*/
private $excluded_field_types = [ 'message', 'accordion', 'tab' ];

/**
* Indentation character
*
* @var string
*/
private $indentation_char = "\t";

/**
* Indentation count
*
* @var int
*/
private $indentation = 1;

/**
* Constructor
*
Expand Down Expand Up @@ -141,12 +162,23 @@ public function render_field_group_settings( $field_group ) {
'prefix' => 'acf_field_group',
'value' => isset( $field_group['block_container_class'] ) ? $field_group['block_container_class'] : $container_class,
] );

acf_render_field_wrap( [
'label' => 'Use Inner Blocks',
'instructions' => 'Will add InnerBlocks element to the template',
'type' => 'true_false',
'name' => 'inner_blocks',
'prefix' => 'acf_field_group',
'value' => false,
'ui' => false,
] );

}

/**
* Initiates Block Loader
*
* @action acf/update_field_group 5
* @action acf/update_field_group 15
*
* @since 1.0.0
* @param array $field_group Field group params.
Expand Down Expand Up @@ -215,28 +247,34 @@ public function update_field_group( $field_group ) {
$fields_markup[] = $this->get_field_markup( $field );
}

$class = $field_group['block_container_class'] ? " class=\"{$field_group['block_container_class']}\"" : null;
// Remove empty markup.
$fields_markup = array_filter( $fields_markup );

// Add inner blocks tag.
if ( $field_group['inner_blocks'] ) {
$fields_markup[] = '<InnerBlocks />';
}

$template = $this->package_fs->get_contents( 'block.php' );
$template = str_replace(
[
'{COMMENT}',
'{FIELDS}',
'{CLASS}',
'{CSS_CLASS}',
],
[
substr( implode( "\n", $comment ), 3 ),
implode( "\n", $fields_markup ),
$class,
$field_group['block_container_class'],
],
$template
);

$this->theme_fs->put_contents( "{$this->config['blocks_dir']}/{$slug}.php", $template );
$this->theme_fs->put_contents( "{$this->config['blocks_dir']}/{$slug}.php", preg_replace( '/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/', "\n\n", $template ) );

// Create block scss partial.
if ( is_string( $this->config['scss_dir'] ) ) {
$scss = ".block.$slug {\n\n}";
$scss = ".block.$slug {\n/* stylelint-disable */\n}";

if ( $this->maybe_mkdir( $this->config['scss_dir'] ) ) {
$this->theme_fs->put_contents( "{$this->config['scss_dir']}/_{$slug}.scss", $scss );
Expand Down Expand Up @@ -314,12 +352,18 @@ private function maybe_mkdir( $dir ) {
* @return string
*/
private function get_field_markup( $field ) {
if ( in_array( $field['type'], $this->excluded_field_types, true ) ) {
return '';
}

$markup_file = "fields/{$field['type']}.php";
$markup_file = $this->package_fs->exists( $markup_file ) ? $markup_file : 'fields/default.php';
$markup = $this->package_fs->get_contents( $markup_file );
$subfields = [];

if ( 'repeater' === $field['type'] ) {
$this->indentation++;

foreach ( $field['sub_fields'] as $sub_field ) {
$subfields[] = str_replace(
[
Expand All @@ -333,20 +377,34 @@ private function get_field_markup( $field ) {
$this->get_field_markup( $sub_field )
);
}

$this->indentation--;
}

$markup = str_replace(
[
'{name}',
'{subfields}',
"\n",
],
[
$field['name'],
implode( "\n", $subfields ),
"\n" . $this->indentation(),
],
$markup
);

return $markup;
return $this->indentation() . $markup;
}

/**
* Gets current indentation
*
* @since 1.0.3
* @return string
*/
private function indentation() {
return str_repeat( $this->indentation_char, $this->indentation );
}
}
2 changes: 1 addition & 1 deletion src/block.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

?>

<div{CLASS}>
<div class="{CSS_CLASS}">

{FIELDS}

Expand Down

0 comments on commit 730ec0a

Please sign in to comment.