diff --git a/includes/js/test-block.js b/includes/js/test-block.js index f9b88a0..ec702e5 100644 --- a/includes/js/test-block.js +++ b/includes/js/test-block.js @@ -186,6 +186,10 @@ DFIGBlockObject.params = typeof DisplayFeaturedImageTestBlock === 'undefined' ? '' : DisplayFeaturedImageTestBlock; if ( typeof DFIGBlockObject.params !== 'undefined' ) { - DFIGBlockObject.init( DFIGBlockObject.params ); + Object.keys( DFIGBlockObject.params ).forEach( function ( key, index ) { + if ( DFIGBlockObject.params.hasOwnProperty( key ) ) { + DFIGBlockObject.init( DFIGBlockObject.params[ key ] ); + } + } ); } } )( wp ); diff --git a/includes/js/test-block.min.js b/includes/js/test-block.min.js index 2c80c7d..6d2f3dd 100644 --- a/includes/js/test-block.min.js +++ b/includes/js/test-block.min.js @@ -1 +1 @@ -!function(e,t){"use strict";const n={el:e.element.createElement};n.init=function(t){const o=e.blocks.registerBlockType,l=e.components.ServerSideRender,i=e.editor.InspectorControls;o(t.block,{title:t.title,description:t.description,keywords:t.keywords,icon:t.icon,category:t.category,supports:{html:!1},getEditWrapperProps:({blockAlignment:e})=>({"data-align":e}),edit:o=>{const{attributes:r,setAttributes:s}=o,c=e.element.Fragment,a=e.editor.BlockControls,u=e.editor.BlockAlignmentToolbar;return[n.el(l,{block:t.block,attributes:r}),n.el(c,null,n.el(a,null,n.el(u,{value:r.blockAlignment,controls:["wide","full"],onChange:e=>{s({blockAlignment:e})}}))),n.el(i,{},function(t,o){const l=[],i=e.components.PanelBody;return Object.keys(o.panels).forEach(function(r,s){if(o.panels.hasOwnProperty(r)){const c=o.panels[r];l[s]=n.el(i,{title:c.title,initialOpen:c.initialOpen,className:"scriptless-panel-"+r},function(t,o){const l=[];return Object.keys(o).forEach(function(i,r){if(o.hasOwnProperty(i)){if(-1!==["blockAlignment","className"].indexOf(i))return;const s=o[i],c=function(t,n){const{TextControl:o,SelectControl:l,RangeControl:i,CheckboxControl:r,TextareaControl:s}=e.components,c=o;return"select"===t?l:"number"===t&&"number"===n?i:"checkbox"===t?r:"textarea"===t?s:c}(s.method,s.type);l[r]=n.el(c,function(e,t,n){const{attributes:o,setAttributes:l}=n,i={label:t.label,value:o[e],className:"displayfeaturedimagegenesis-"+e,onChange:t=>{l({[e]:t})}};return"select"===t.method?i.options=t.options:"number"===t.method?(i.min=t.min,i.max=t.max,"number"!==t.type?i.type="number":i.initialPosition=t.min):"checkbox"===t.method&&(i.checked=o[e]),i}(i,s,t))}}),l}(t,c.attributes))}}),l}(o,t))]},save:e=>null})},n.params="undefined"==typeof DisplayFeaturedImageTestBlock?"":DisplayFeaturedImageTestBlock,void 0!==n.params&&n.init(n.params)}(wp); \ No newline at end of file +!function(e,t){"use strict";const n={el:e.element.createElement};n.init=function(t){const o=e.blocks.registerBlockType,l=e.components.ServerSideRender,r=e.editor.InspectorControls;o(t.block,{title:t.title,description:t.description,keywords:t.keywords,icon:t.icon,category:t.category,supports:{html:!1},getEditWrapperProps:({blockAlignment:e})=>({"data-align":e}),edit:o=>{const{attributes:i,setAttributes:s}=o,a=e.element.Fragment,c=e.editor.BlockControls,m=e.editor.BlockAlignmentToolbar;return[n.el(l,{block:t.block,attributes:i}),n.el(a,null,n.el(c,null,n.el(m,{value:i.blockAlignment,controls:["wide","full"],onChange:e=>{s({blockAlignment:e})}}))),n.el(r,{},function(t,o){const l=[],r=e.components.PanelBody;return Object.keys(o.panels).forEach(function(i,s){if(o.panels.hasOwnProperty(i)){const a=o.panels[i];l[s]=n.el(r,{title:a.title,initialOpen:a.initialOpen,className:"scriptless-panel-"+i},function(t,o){const l=[];return Object.keys(o).forEach(function(r,i){if(o.hasOwnProperty(r)){if(-1!==["blockAlignment","className"].indexOf(r))return;const s=o[r],a=function(t,n){const{TextControl:o,SelectControl:l,RangeControl:r,CheckboxControl:i,TextareaControl:s}=e.components,a=o;return"select"===t?l:"number"===t&&"number"===n?r:"checkbox"===t?i:"textarea"===t?s:a}(s.method,s.type);l[i]=n.el(a,function(e,t,n){const{attributes:o,setAttributes:l}=n,r={label:t.label,value:o[e],className:"displayfeaturedimagegenesis-"+e,onChange:t=>{l({[e]:t})}};return"select"===t.method?r.options=t.options:"number"===t.method?(r.min=t.min,r.max=t.max,"number"!==t.type?r.type="number":r.initialPosition=t.min):"checkbox"===t.method&&(r.checked=o[e]),r}(r,s,t))}}),l}(t,a.attributes))}}),l}(o,t))]},save:e=>null})},n.params="undefined"==typeof DisplayFeaturedImageTestBlock?"":DisplayFeaturedImageTestBlock,void 0!==n.params&&Object.keys(n.params).forEach(function(e,t){n.params.hasOwnProperty(e)&&n.init(n.params[e])})}(wp); \ No newline at end of file diff --git a/includes/widgets/class-displayfeaturedimagegenesis-output-block.php b/includes/widgets/class-displayfeaturedimagegenesis-output-block.php index 8139b21..5c30912 100644 --- a/includes/widgets/class-displayfeaturedimagegenesis-output-block.php +++ b/includes/widgets/class-displayfeaturedimagegenesis-output-block.php @@ -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 = '