diff --git a/CHANGELOG.md b/CHANGELOG.md index d457f0b..cf9b232 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,17 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). -[Unreleased] +## [Unreleased] + +### Added + +- ACF Codifier Components + + Components are reusable field sets. + + Components can be transformed into any type of groupable field with component factories. + * `Geniem\ACF\Factory\Block`: A factory for creating ACF Gutenberg blocks with Codifier components. + + Component rendering functionality implementing the `Geniem\ACF\Interfaces\Renderer` interface. + * `Geniem\ACF\Renderer\PHP`: Uses PHP files as templates. + * `Geniem\ACF\Renderer\Dust`: Uses Dust.js template files. ## [1.41.2] @@ -228,6 +238,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Added - Added the ability to use Allow null setting with the Radio field. +>>>>>>> master ## [1.20.0] - 2019-10-10 diff --git a/src/Component.php b/src/Component.php new file mode 100644 index 0000000..66fcacb --- /dev/null +++ b/src/Component.php @@ -0,0 +1,242 @@ +name; + } + + /** + * Getter for the title. + * + * @return string + */ + public function get_title() : string { + + return $this->title; + } + + /** + * Getter for the description. + * + * @return string + */ + public function get_description() : string { + + return $this->description; + } + + /** + * Constructor method + * + * @param array|null $args Arguments to give to the block on creation. + */ + public function __construct( ?array $args = null ) {} + + /** + * Set the renderer for the component. + * + * @param Renderer $renderer The renderer. + */ + public function set_renderer( Renderer $renderer ) { + $this->renderer = $renderer; + } + + /** + * Getter for the component renderer. + * + * @return Renderer + * @throws Exception An exception is thrown if the renderer is not set + * and this method is called. + */ + public function get_renderer(): Renderer { + + if ( empty( $this->renderer ) ) { + // The extending class must implement this method + // if the renderer is not set. + throw new Exception( 'You must implement get_renderer()' ); + } + return $this->renderer; + } + + /** + * Getter for the category. + * + * @return string + */ + public function get_category() : string { + return $this->category; + } + + /** + * Getter for the icon. + * + * @return string + */ + public function get_icon() : string { + return $this->icon; + } + + /** + * Getter for the keywords. + * + * @return array + */ + public function get_keywords() : array { + return $this->keywords; + } + + /** + * Getter for the post types. + * + * @return array + */ + public function get_post_types() : array { + return $this->post_types; + } + + /** + * Getter for the display mode. + * + * @return string + */ + public function get_mode() : string { + return $this->mode; + } + + /** + * Getter for the default block alignment. + * + * @return string + */ + public function get_align() : string { + return $this->align; + } + + /** + * Getter for the supported features of the block. + * + * @return array + */ + public function get_supports() : array { + return $this->supports; + } +} diff --git a/src/Factory/Block.php b/src/Factory/Block.php new file mode 100644 index 0000000..3eb9f70 --- /dev/null +++ b/src/Factory/Block.php @@ -0,0 +1,131 @@ +component = $component; + + if ( ! function_exists( 'acf_register_block' ) ) { + throw new Exception( 'Advanced Custom Fields version 5.8.0 or greater must be activated!' ); + } + } + + /** + * Registers the ACF Gutenberg block with the component data. + * + * @return array The registered block data. + * + * @throws Exception ACF Codifier exception. + */ + public function create() { + $this->register_field_group(); + $block = $this->register_block(); + return $block; + } + + /** + * Register the ACF field group for the block. + * + * @throws Exception ACF Codifier exception. + */ + protected function register_field_group() { + + // Define a field group and set it to use the component fields. + $field_group = new Group( $this->component->get_title(), $this->component->get_name() ); + + $rule_group = new RuleGroup(); + $rule_group->add_rule( 'block', '==', 'acf/' . $this->component->get_name() ); + + $field_group->add_rule_group( $rule_group ); + + $field_group->add_fields( $this->component->get_fields() ); + + $field_group->register(); + } + + /** + * Register the ACF block. + * + * @return array + */ + protected function register_block() { + $args = [ + 'name' => $this->component->get_name(), + 'title' => $this->component->get_title(), + 'description' => $this->component->get_description(), + 'render_callback' => [ $this, 'render' ], + 'category' => $this->component->get_category(), + 'icon' => $this->component->get_icon(), + 'keywords' => $this->component->get_keywords(), + 'mode' => $this->component->get_mode(), + 'align' => $this->component->get_align(), + 'supports' => $this->component->get_supports(), + ]; + + if ( ! empty( $this->component->get_post_types() ) ) { + $args['post_types'] = $this->component->get_post_types(); + } + + // Register the ACF Block. + $block = acf_register_block( $args ); + + return $block; + } + + /** + * The render callback method for ACF blocks. + * Passes the data to the defined renderer and + * prints out the rendered markup. + * + * @param array $block The ACF block data. + */ + public function render( array $block = [] ) { + $renderer = $this->component->get_renderer(); + + $data = get_fields(); + + if ( method_exists( $this->component, 'data' ) ) { + $data = $this->component->data( $data ); + } + + echo $renderer->render( + [ + 'data' => $data, + 'block' => $block, + ] + ); + } + +} diff --git a/src/Interfaces/Component.php b/src/Interfaces/Component.php new file mode 100644 index 0000000..9c769b5 --- /dev/null +++ b/src/Interfaces/Component.php @@ -0,0 +1,72 @@ +renderTemplate( $compiled, $fields['data'] ); } -} \ No newline at end of file +} diff --git a/src/Renderer/PHP.php b/src/Renderer/PHP.php index f6bb5af..4a7c3a5 100644 --- a/src/Renderer/PHP.php +++ b/src/Renderer/PHP.php @@ -55,4 +55,4 @@ public function render( array $fields ) : string { } -} \ No newline at end of file +}