use standard block.json method with ACF blocks
This commit is contained in:
@@ -8,14 +8,15 @@ class Dynamic
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
add_filter('acf/load_field/name=colour', [ $this, 'load_colours' ]);
|
||||
add_filter('acf/load_field/name=bg_colour', [ $this, 'load_colours' ]);
|
||||
add_filter('acf/load_field/name=tint', [ $this, 'load_tints' ]);
|
||||
add_filter('acf/load_field/name=bg_tint', [ $this, 'load_tints' ]);
|
||||
add_filter('acf/load_field/name=colour', [ $this, 'load_colours' ]);
|
||||
add_filter('acf/load_field/name=bg_colour', [ $this, 'load_colours' ]);
|
||||
add_filter('acf/load_field/name=tint', [ $this, 'load_tints' ]);
|
||||
add_filter('acf/load_field/name=bg_tint', [ $this, 'load_tints' ]);
|
||||
add_filter('acf/load_field/name=container_width', [ $this, 'container_width' ]);
|
||||
add_filter('acf/load_field/name=fontawesome_regular', [ $this, 'load_fontawesome_regular_icons' ]);
|
||||
add_filter('acf/load_field/name=fontawesome_solid', [ $this, 'load_fontawesome_solid_icons' ]);
|
||||
add_filter('acf/load_field/name=fontawesome_brands', [ $this, 'load_fontawesome_brand_icons' ]);
|
||||
add_action('acf/input/admin_footer', [$this, 'colour_ui'] );
|
||||
add_filter('acf/load_field/name=fontawesome_solid', [ $this, 'load_fontawesome_solid_icons' ]);
|
||||
add_filter('acf/load_field/name=fontawesome_brands', [ $this, 'load_fontawesome_brand_icons' ]);
|
||||
add_action('acf/input/admin_footer', [ $this, 'colour_ui' ]);
|
||||
}
|
||||
|
||||
public function load_colours( $field )
|
||||
@@ -25,7 +26,9 @@ class Dynamic
|
||||
|
||||
$colours = $colour->values();
|
||||
|
||||
$field['choices'] = [];
|
||||
$field['choices'] = [
|
||||
'0' => __('None', 'badegg'),
|
||||
];
|
||||
|
||||
foreach($colours as $slug => $hex):
|
||||
$field['choices'][$slug] = '<i class="fas fa-circle" style="color: '. $hex .'"></i> ' . @$NameThatColour->name($hex)['name'];
|
||||
@@ -47,13 +50,27 @@ class Dynamic
|
||||
$field['choices'][$slug] = ucfirst($slug);
|
||||
|
||||
else:
|
||||
$field['choices'][0] = 'None';
|
||||
$field['choices'][0] = __('None', 'badegg');
|
||||
endif;
|
||||
endforeach;
|
||||
|
||||
return $field;
|
||||
}
|
||||
|
||||
public function container_width( $field )
|
||||
{
|
||||
$field['choices'] = [
|
||||
0 => 'Auto',
|
||||
'narrow' => __('Narrow', 'badegg'),
|
||||
'small' => __('Small', 'badegg'),
|
||||
'medium' => __('Medium', 'badegg'),
|
||||
'large' => __('Large', 'badegg'),
|
||||
'full' => __('Edge to edge', 'badegg'),
|
||||
];
|
||||
|
||||
return $field;
|
||||
}
|
||||
|
||||
public function load_fontawesome_regular_icons( $field )
|
||||
{
|
||||
$field['choices'] = [];
|
||||
|
||||
@@ -40,7 +40,7 @@ class Colour
|
||||
endforeach;
|
||||
endif;
|
||||
|
||||
$values['0'] = '#FFFFFF';
|
||||
$values['white'] = '#FFFFFF';
|
||||
$values['black'] = '#000000';
|
||||
|
||||
return $values;
|
||||
|
||||
@@ -3,56 +3,94 @@
|
||||
namespace App\Utilities;
|
||||
|
||||
class CssClasses {
|
||||
public function section($props = [])
|
||||
public function section($props = [], $name = 'unnamed', $knockout = false)
|
||||
{
|
||||
$Colour = new Colour;
|
||||
$hex = $Colour->name2hex(@$props['bg_colour'], @$props['bg_tint']);
|
||||
$defaults = [
|
||||
'padding_top' => null,
|
||||
'padding_bottom' => null,
|
||||
'bg_colour' => null,
|
||||
'bg_tint' => null,
|
||||
'contrast' => null,
|
||||
'bg_image' => null,
|
||||
];
|
||||
|
||||
$pattern = @$props['pattern'];
|
||||
$pattern_top = @$props['pattern_top'];
|
||||
$pattern_bottom = @$props['pattern_bottom'];
|
||||
$props = wp_parse_args($props, $defaults);
|
||||
|
||||
$Colour = new Colour;
|
||||
$hex = $Colour->name2hex($props['bg_colour'], $props['bg_tint']);
|
||||
|
||||
$classes = [
|
||||
'section',
|
||||
'section-' . $props['name'],
|
||||
// 'section-' . str_replace('acf/', '', $props['name']),
|
||||
'bg-' . $this->colourTint([
|
||||
'colour' => @$props['bg_colour'],
|
||||
'tint' => @$props['bg_tint'],
|
||||
]),
|
||||
'section-' . str_replace('/', '-', $name),
|
||||
];
|
||||
|
||||
if($Colour->is_dark($hex) && $this->is_knockout_block($props['name']))
|
||||
if($props['bg_colour'])
|
||||
$classes[] = 'bg-' . $this->colourTint([
|
||||
'colour' => $props['bg_colour'],
|
||||
'tint' => $props['bg_tint'],
|
||||
]);
|
||||
|
||||
if(($knockout && $Colour->is_dark($hex) || $props['contrast'] == 'light'))
|
||||
$classes[] = 'knockout';
|
||||
|
||||
if(@$props['padding_top'])
|
||||
if($props['padding_top'])
|
||||
$classes[] = 'section-zero-top';
|
||||
|
||||
if(@$props['padding_bottom'])
|
||||
if($props['padding_bottom'])
|
||||
$classes[] = 'section-zero-bottom';
|
||||
|
||||
if($pattern):
|
||||
if($pattern == 'both'):
|
||||
$classes[] = 'pattern-top';
|
||||
$classes[] = 'pattern-bottom';
|
||||
|
||||
else:
|
||||
$classes[] = 'pattern-' . $pattern;
|
||||
|
||||
endif;
|
||||
|
||||
if(in_array($pattern, ['top', 'both']))
|
||||
$classes[] = 'pattern-top-' . $this->colourTint($pattern_top);
|
||||
|
||||
if(in_array($pattern, ['bottom', 'both']))
|
||||
$classes[] = 'pattern-bottom-' . $this->colourTint($pattern_bottom);
|
||||
|
||||
endif;
|
||||
|
||||
if(@$props['bg_image'])
|
||||
if($props['bg_image'])
|
||||
$classes[] = "bg-watermarked";
|
||||
|
||||
if(@$props['className']) $args = array_merge($classes, explode(' ', $props['className']));
|
||||
return $classes;
|
||||
}
|
||||
|
||||
public function container($args = [], $bg_props = [])
|
||||
{
|
||||
$args = wp_parse_args($args, [
|
||||
'width' => null,
|
||||
'location' => null,
|
||||
'section' => false,
|
||||
'align' => null,
|
||||
'wysiwyg' => false,
|
||||
]);
|
||||
|
||||
$bg_props = wp_parse_args($bg_props, [
|
||||
'bg_colour' => null,
|
||||
'bg_tint' => null,
|
||||
'contrast' => null,
|
||||
]);
|
||||
|
||||
$Colour = new Colour;
|
||||
$hex = $Colour->name2hex($bg_props['bg_colour'], $bg_props['bg_tint']);
|
||||
|
||||
$classes = [
|
||||
'container',
|
||||
];
|
||||
|
||||
if($args['width'])
|
||||
$classes[] = 'container-' . $args['width'];
|
||||
|
||||
if($args['location'])
|
||||
$classes[] = $args['location'];
|
||||
|
||||
if($args['section'])
|
||||
$classes[] = 'section';
|
||||
|
||||
if(str_contains($args['location'], 'intro'))
|
||||
$classes[] = 'section-zero-top';
|
||||
|
||||
if(str_contains($args['location'], 'footer'))
|
||||
$classes[] = 'section-zero-bottom';
|
||||
|
||||
if($args['wysiwyg'])
|
||||
$classes[] = 'wysiwyg';
|
||||
|
||||
if($args['align'])
|
||||
$classes[] = 'align-' . $args['align'];
|
||||
|
||||
if(($Colour->is_dark($hex) || $bg_props['contrast'] == 'light'))
|
||||
$classes[] = 'knockout';
|
||||
|
||||
return $classes;
|
||||
}
|
||||
@@ -94,7 +132,7 @@ class CssClasses {
|
||||
public function is_knockout_block($name = null)
|
||||
{
|
||||
$blacklist = [
|
||||
'bad-example',
|
||||
'badegg/acfdemo',
|
||||
];
|
||||
|
||||
if(in_array($name, $blacklist)):
|
||||
|
||||
31
app/View/Composers/Blocks.php
Normal file
31
app/View/Composers/Blocks.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\View\Composers;
|
||||
|
||||
use Roots\Acorn\View\Composer;
|
||||
use App\Utilities;
|
||||
|
||||
class Blocks extends Composer
|
||||
{
|
||||
/**
|
||||
* List of views served by this composer.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $views = [
|
||||
'layouts.block-acf',
|
||||
'partials.block-*',
|
||||
];
|
||||
|
||||
/**
|
||||
* Data to be passed to view before rendering.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function with()
|
||||
{
|
||||
return [
|
||||
'CssClasses' => new Utilities\CssClasses,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,17 @@
|
||||
|
||||
namespace App\Blocks;
|
||||
|
||||
add_filter('block_type_metadata', function($metadata){
|
||||
$name = $metadata['name'];
|
||||
|
||||
if (str_starts_with($name, 'core/') ) {
|
||||
unset($metadata['supports']['color']);
|
||||
unset($metadata['supports']['typography']);
|
||||
}
|
||||
|
||||
return $metadata;
|
||||
});
|
||||
|
||||
// Disable all core blocks except what we need as inner blocks
|
||||
// resources/js/editor.js handles hiding the inner blocks at the top level
|
||||
add_action('allowed_block_types_all', __NAMESPACE__ . '\\list_allowed', 100, 2);
|
||||
@@ -152,14 +163,17 @@ function render_acf($block, $content = '', $is_preview = false, $post_id = 0, $w
|
||||
$slug = basename($block['name']);
|
||||
$block['slug'] = $slug;
|
||||
|
||||
$blade = \Roots\view("blocks.{$slug}.render", [
|
||||
'block' => $block,
|
||||
'content' => $content,
|
||||
'is_preview' => $is_preview,
|
||||
'post_id' => $post_id,
|
||||
'wp_block' => $wp_block,
|
||||
'context' => $context,
|
||||
]);
|
||||
$blade = \Roots\view(
|
||||
"blocks.{$slug}.render",
|
||||
[
|
||||
'block' => $block,
|
||||
'content' => $content,
|
||||
'is_preview' => $is_preview,
|
||||
'post_id' => $post_id,
|
||||
'wp_block' => $wp_block,
|
||||
'context' => $context,
|
||||
],
|
||||
);
|
||||
|
||||
if($blade) {
|
||||
echo $blade;
|
||||
@@ -176,7 +190,3 @@ function render_acf($block, $content = '', $is_preview = false, $post_id = 0, $w
|
||||
<?php echo ob_get_clean();
|
||||
}
|
||||
}
|
||||
|
||||
add_action('wp_footer', function(){
|
||||
echo '<pre>',print_r(list_allowed()),'</pre>';
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user