Skip to content

Instantly share code, notes, and snippets.

@lucprincen
Last active September 11, 2019 11:45
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucprincen/4dd139f1b2ca794485dbfe4cdd3fa4de to your computer and use it in GitHub Desktop.
Save lucprincen/4dd139f1b2ca794485dbfe4cdd3fa4de to your computer and use it in GitHub Desktop.
<?php
namespace YourPlugin\Blocks;
abstract class Block{
/**
* Block title
*
* @var string
*/
protected $title = '';
/**
* Properties for this block
*
* @var array
*/
protected $properties = [];
/**
* Constructor;
*/
public function __construct( $props = [])
{
$this->properties = wp_parse_args( $props, $this->get_defaults() );
}
/**
* Register this block:
*
* @return void
*/
public function register()
{
add_action( 'acf/init', function(){
if( function_exists( 'acf_register_block_type' ) ){
acf_register_block_type([
'name' => $this->get_slug(),
'title' => $this->title,
'description' => $this->prop( 'description' ),
'render_template' => $this->get_template(),
'category' => $this->prop( 'category' ),
'icon' => $this->prop( 'icon' ),
'keywords' => $this->prop( 'keywords' )
]);
}
});
if( function_exists( 'acf_add_local_field_group' ) ){
acf_add_local_field_group([
'key' => 'block_fields_'.$this->get_slug(),
'title' => $this->title,
'fields' => $this->get_sanitized_fields(),
'location' => [[[
'param' => 'block',
'operator' => '==',
'value' => 'acf/'.$this->get_slug()
]]],
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
]);
}
}
/**
* Return the fields
*
* @return array
*/
public function get_fields()
{
return [];
}
/**
* Get all fields and add defaults to them
*
* @return array
*/
public function get_sanitized_fields( $fields = null )
{
if( is_null( $fields ) ){
$fields = $this->get_fields();
}
foreach( $fields as $key => $field ){
if( !isset( $field['key'] ) ){
$fields[$key]['key'] = $field['name'];
}
if( isset( $field['sub_fields'] ) && !empty( $field['sub_fields'] ) ){
$fields[$key]['sub_fields'] = $this->get_sanitized_fields( $field['sub_fields'] );
}
}
return $fields;
}
/**
* Return a property
*
* @return mixed
*/
public function prop( $key, $default = null )
{
if( isset( $this->properties[ $key ] ) ){
return $this->properties[ $key ];
}
return $default;
}
/**
* Return the slug for this block
*
* @return string
*/
public function get_slug()
{
return sanitize_title( $this->title );
}
/**
* Return the template for this block:
*
* @return string
*/
public function get_template()
{
return 'blocks/'.$this->get_slug().'.php';
}
/**
* Return default properties
*
* @return array
*/
public function get_defaults()
{
$defaults = [
'description' => '',
'category' => 'common',
'icon' => 'admin-comments',
'keywords' => []
];
return apply_filters( 'my_plugin_custom_block_defaults', $defaults, $this );
}
}
<div class="example-block">
<h3><?php echo get_field( 'example' );?></h3>
<?php if( get_field('button') && is_array( get_field( 'button' ) ) ):
$button = get_field( 'button' );
?>
<a href="<?php echo esc_attr( $button['link'] );?>" class="button primary">
<?php echo esc_html( $link['title'] );?>
</a>
<?php endif;?>
</div>
<?php
namespace YourPlugin\Blocks;
class ExampleBlock extends Block{
/**
* Title of this block
* Determens how this block is added, saved and which template to use.
*
* Block name: "Example"
* Block slug/id: example
* Template: /blocks/example.php
*
* @var string
*/
protected $title = 'Example';
/**
* Return the fields
*
* @return Array
*/
public function get_fields()
{
return [
[
'label' => 'An example field',
'name' => 'example', //field name for get_field()
'type' => 'text' //type of field to show
],
[
'label' => 'Button',
'name' => 'button',
'type' => 'link'
]
];
}
}
@lucprincen
Copy link
Author

lucprincen commented Sep 10, 2019

And by adding a get_defaults() function to your new blocks, you can control some of its settings:

  /**
    * Return default properties
    *
    * @return array
    */
    public function get_defaults()
    {
        return [
            'description' => 'This is a custom block description',
            'category' => 'common',
            'icon' => 'yes',
            'keywords' => [ 'myblock', 'testing' ]
        ];
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment