Initial work to get multiple blocks out of one go

This commit is contained in:
Robin Cornett
2019-06-23 17:37:16 -04:00
parent 477bfb08e7
commit ae0f537416
3 changed files with 113 additions and 49 deletions
@@ -4,7 +4,7 @@
*/
/**
* Class ScriptlessSocialSharingOutputBlock
* Class DisplayFeaturedImageGenesisOutputBlock
*/
class DisplayFeaturedImageGenesisOutputBlock {
@@ -13,12 +13,12 @@ class DisplayFeaturedImageGenesisOutputBlock {
*
* @var string
*/
protected $name = 'displayfeaturedimagegenesis/post-type';
protected $name = 'displayfeaturedimagegenesis/';
/**
* @var string
*/
protected $block = 'displayfeaturedimagegenesis-post-type';
protected $block = 'displayfeaturedimagegenesis-';
/**
* The plugin setting.
@@ -31,17 +31,56 @@ class DisplayFeaturedImageGenesisOutputBlock {
*/
public function init() {
$this->register_script_style();
register_block_type(
$this->name,
array(
'editor_script' => $this->block . '-block',
'attributes' => $this->fields(),
'render_callback' => array( $this, 'render' ),
)
);
foreach ( $this->blocks() as $block => $data ) {
if ( empty( $data['nickname'] ) || ! is_callable( array( $this, "render_{$data['nickname']}" ) ) ) {
continue;
}
register_block_type(
"{$this->name}{$block}",
array(
'editor_script' => "{$this->block}block",
'attributes' => $this->fields( $block ),
'render_callback' => array( $this, "render_{$data['nickname']}" ),
)
);
}
add_action( 'enqueue_block_editor_assets', array( $this, 'localize' ) );
}
/**
* Get the list of blocks to create.
* @return array
*/
private function blocks() {
return array(
'term' => array(
'title' => __( 'Display Featured Term Image', 'display-featured-image-genesis' ),
'description' => __( 'Display a featured term', 'display-featured-image-genesis' ),
'keywords' => array(
__( 'Term', 'display-featured-image-genesis' ),
__( 'Featured Image', 'display-featured-image-genesis' ),
),
),
'author' => array(
'title' => __( 'Display Featured Author Profile', 'display-featured-image-genesis' ),
'description' => __( 'Display a featured author', 'display-featured-image-genesis' ),
'keywords' => array(
__( 'Author', 'display-featured-image-genesis' ),
__( 'Featured Image', 'display-featured-image-genesis' ),
),
),
'post-type' => array(
'title' => __( 'Display Featured Post Type Archive Image', 'display-featured-image-genesis' ),
'description' => __( 'Display a featured content type', 'display-featured-image-genesis' ),
'keywords' => array(
__( 'Post Type', 'display-featured-image-genesis' ),
__( 'Featured Image', 'display-featured-image-genesis' ),
),
'nickname' => 'cpt',
),
);
}
/**
* Render the widget in a container div.
*
@@ -49,14 +88,14 @@ class DisplayFeaturedImageGenesisOutputBlock {
*
* @return string
*/
public function render( $atts ) {
public function render_cpt( $atts ) {
$atts = wp_parse_args( $atts, include 'fields/cpt-defaults.php' );
$post_type = get_post_type_object( $atts['post_type'] );
if ( ! $post_type ) {
return '';
}
$classes = $this->get_block_classes( $atts );
$classes = $this->get_block_classes( $atts, 'post-type' );
include plugin_dir_path( dirname( __FILE__ ) ) . 'output/class-displayfeaturedimagegenesis-output-cpt.php';
$output = '<div class="' . implode( ' ', $classes ) . '">';
ob_start();
@@ -74,9 +113,9 @@ class DisplayFeaturedImageGenesisOutputBlock {
*
* @return array
*/
private function get_block_classes( $atts ) {
array(
'wp-block-' . $this->block,
private function get_block_classes( $atts, $block_id ) {
$classes = array(
"wp-block-{$this->block}{$block_id}",
);
if ( ! empty( $atts['className'] ) ) {
$classes[] = $atts['className'];
@@ -93,12 +132,12 @@ class DisplayFeaturedImageGenesisOutputBlock {
*/
public function register_script_style() {
$minify = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : ' . min';
$version = '3.2.0';
$version = displayfeaturedimagegenesis_get()->version;
if ( ! $minify ) {
$version .= current_time( 'gmt' );
}
wp_register_script(
$this->block . '-block',
"{$this->block}block",
plugin_dir_url( dirname( __FILE__ ) ) . "js/test-block{$minify}.js",
array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-editor' ),
$version,
@@ -110,7 +149,7 @@ class DisplayFeaturedImageGenesisOutputBlock {
* Localize.
*/
public function localize() {
wp_localize_script( $this->block . '-block', 'DisplayFeaturedImageTestBlock', $this->get_localization_data() );
wp_localize_script( "{$this->block}block", 'DisplayFeaturedImageTestBlock', $this->get_localization_data() );
}
/**
@@ -118,33 +157,46 @@ class DisplayFeaturedImageGenesisOutputBlock {
* @return array
*/
protected function get_localization_data() {
return array(
'block' => $this->name,
'title' => __( 'Display Featured Image Genesis Post Type', 'scriptless-social-sharing' ),
'description' => __( 'featured image.', 'scriptless-social-sharing' ),
'keywords' => array(
__( 'image', 'scriptless-social-sharing' ),
__( 'featured', 'scriptless-social-sharing' ),
),
'panels' => array(
'main' => array(
'title' => __( 'Block Settings', 'scriptless-social-sharing' ),
'initialOpen' => true,
'attributes' => $this->fields(),
),
),
'icon' => 'format-image',
'category' => 'widgets',
$blocks = $this->blocks();
$common = array(
'icon' => 'format-image',
'category' => 'widgets',
);
$output = array();
foreach ( $blocks as $block => $data ) {
if ( empty( $data['nickname'] ) || ! is_callable( array( $this, "render_{$data['nickname']}" ) ) ) {
continue;
}
$common['panels'] = array(
'main' => array(
'title' => __( 'Block Settings', 'display-featured-image-genesis' ),
'initialOpen' => true,
'attributes' => $this->fields( $block ),
),
);
$common['block'] = "{$this->name}{$block}";
if ( ! empty( $data['nickname'] ) ) {
$block = $data['nickname'];
}
$output[ $block ] = array_merge(
$data,
$common
);
}
return $output;
}
/**
* Get the fields for the block.
*
* @param $block
*
* @return array
*/
private function fields() {
private function fields( $block ) {
$output = array();
foreach ( $this->get_all_fields() as $key => $value ) {
foreach ( $this->get_all_fields( $block ) as $key => $value ) {
if ( ! empty( $value['args']['id'] ) ) {
$key = $value['args']['id'];
}
@@ -154,14 +206,11 @@ class DisplayFeaturedImageGenesisOutputBlock {
return $output;
}
private function get_all_fields() {
$form = new DisplayFeaturedImageGenesisWidgetsForm( $this, array() );
$fields = array_merge(
include 'fields/cpt-post_type.php',
include 'fields/text.php',
include 'fields/image.php',
include 'fields/archive.php'
);
private function get_all_fields( $block ) {
if ( 'post-type' === $block ) {
$block = 'cpt';
}
$fields = "{$block}_fields";
$attributes = array_merge(
array(
'blockAlignment' => array(
@@ -181,12 +230,23 @@ class DisplayFeaturedImageGenesisOutputBlock {
),
),
),
$fields
$this->$fields()
);
return $attributes;
}
protected function cpt_fields() {
$form = new DisplayFeaturedImageGenesisWidgetsForm( $this, array() );
return array_merge(
include 'fields/cpt-post_type.php',
include 'fields/text.php',
include 'fields/image.php',
include 'fields/archive.php'
);
}
/**
* Get an array of attributes for an individual field.
*