From d3513b1cdf930ed400cb6e7d441fb0f6b01b9c28 Mon Sep 17 00:00:00 2001 From: Jan Widmer Date: Mon, 4 Jul 2022 11:08:11 +0200 Subject: [PATCH] enhance to offer a multiple option for post, page and taxonomy --- .node-version | 2 +- package.json | 2 +- src/assets/js/select-dynamic-control.jsx | 11 ++++--- src/assets/js/select-dynamic.jsx | 17 ++++++++++ src/controls/select-dynamic.php | 32 ++++++++++++++++++- src/languages/lzb-select-dynamic.json | 9 ++++++ src/languages/lzb-select-dynamic.pot | 40 +++++++++++++++--------- src/readme.md | 13 +++++++- 8 files changed, 104 insertions(+), 22 deletions(-) diff --git a/.node-version b/.node-version index 07c142f..d928989 100644 --- a/.node-version +++ b/.node-version @@ -1 +1 @@ -16.13.1 +16.15.1 diff --git a/package.json b/package.json index ffa76fa..3aecdd8 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "lzb-select-dynamic", "title": "Lazy Blocks: Select Dynamic Control", - "version": "2.2.1", + "version": "2.4.0", "description": "lazy-blocks custom control to offer a dynamic select containing wordpress posts or categories", "license": "GPL-2.0", "author": "Jan Widmer ", diff --git a/src/assets/js/select-dynamic-control.jsx b/src/assets/js/select-dynamic-control.jsx index ae512ce..24a670b 100644 --- a/src/assets/js/select-dynamic-control.jsx +++ b/src/assets/js/select-dynamic-control.jsx @@ -18,7 +18,7 @@ const postTypesToIgnore = [ class SelectDynamicControl extends Component { render () { - const { label, value, help, entityType, onChange = () => {}, items, } = this.props; + const { label, value, help, entityType, multiple, onChange = () => {}, items, } = this.props; let choices = []; @@ -71,6 +71,7 @@ class SelectDynamicControl extends Component { options={ choices } help={ help } value={ value } + multiple={ multiple } className="lzb-gutenberg-select" onChange={(val) => { onChange(val); @@ -90,19 +91,21 @@ export default compose([ per_page: -1, }; + debugger + if (ownProps.entityType === 'post') { entityKind = 'postType'; - entityName = ownProps.postType || 'post'; // if a postType is given we use it (to get custom posts) + entityName = ownProps.postType || 'post'; // if a postType is given we use it (to get custom posts) } else if (ownProps.entityType === 'page') { entityKind = 'postType'; entityName = 'page'; } else if (ownProps.entityType === 'taxonomy') { entityKind = 'taxonomy'; - entityName = ownProps.taxonomyType || 'category'; // if a taxonomyType is given we use it (to get custom taxonomy) + entityName = ownProps.taxonomyType || 'category'; // if a taxonomyType is given we use it (to get custom taxonomy) } // does only work for pages / taxonomies other than tags as other entities cannot be nested - if ((ownProps.entityType === 'page' || (ownProps.entityType === 'taxonomy' && ownProps.taxonomyType !== 'tag')) && ownProps.parentEntity) { + if ((ownProps.entityType === 'page' || (ownProps.entityType === 'taxonomy' && ownProps.taxonomyType !== 'tag')) && ownProps.parentEntity) { query['parent'] = ownProps.parentEntity; } diff --git a/src/assets/js/select-dynamic.jsx b/src/assets/js/select-dynamic.jsx index 372203f..691d0b6 100644 --- a/src/assets/js/select-dynamic.jsx +++ b/src/assets/js/select-dynamic.jsx @@ -17,6 +17,7 @@ const { PanelBody, BaseControl, RadioControl, + CheckboxControl, } = wp.components; /** @@ -30,6 +31,7 @@ addFilter('lzb.editor.control.select_dynamic.render', 'lzb.editor', (render, pro postType={props.data.post_type} taxonomyType={props.data.taxonomy_type} parentEntity={props.data.parent_entity} + multiple={ 'true' === props.data.multiple } value={props.getValue()} onChange={props.onChange} /> @@ -115,7 +117,22 @@ addFilter('lzb.constructor.control.select_dynamic.settings', 'lzb.constructor', /> )} + + {(props.data.entity_type && (props.data.entity_type === 'post' || props.data.entity_type === 'page' || props.data.entity_type === 'taxonomy')) && ( + + + updateData( { multiple: value ? 'true' : 'false' } ) } + /> + + + )} ); }); diff --git a/src/controls/select-dynamic.php b/src/controls/select-dynamic.php index 5684af6..f11b6ba 100644 --- a/src/controls/select-dynamic.php +++ b/src/controls/select-dynamic.php @@ -50,9 +50,12 @@ public function __construct() { // Optional additional attributes, that will be saved in control data. $this->attributes = array( - 'select_dynamic_custom_attribute' => 'default_value', + 'multiple' => 'false', ); + // Filters. + add_filter( 'lzb/prepare_block_attribute', array( $this, 'filter_lzb_prepare_block_attribute' ), 10, 2 ); + parent::__construct(); } @@ -92,6 +95,33 @@ public function get_script_depends() { public function get_style_depends() { return array( 'ww-lzb-control-select_dynamic' ); } + + /** + * Filter block attribute. + * + * @param string $attribute_data - attribute data. + * @param mixed $control - control data. + * + * @return array filtered attribute data. + */ + public function filter_lzb_prepare_block_attribute( $attribute_data, $control ) { + if ( + ! $control || + ! isset( $control['type'] ) || + $this->name !== $control['type'] || + ! isset( $control['multiple'] ) + ) { + return $attribute_data; + } + + if ( 'true' === $control['multiple'] ) { + $attribute_data['type'] = 'array'; + $attribute_data['items'] = array( 'type' => 'string' ); + $attribute_data['default'] = explode( ',', $attribute_data['default'] ); + } + + return $attribute_data; + } } // Init. diff --git a/src/languages/lzb-select-dynamic.json b/src/languages/lzb-select-dynamic.json index 6ad0df7..808dbe5 100644 --- a/src/languages/lzb-select-dynamic.json +++ b/src/languages/lzb-select-dynamic.json @@ -70,6 +70,15 @@ ], "Allows you to restrict the select options to a certain parent entity, otherwise leave blank": [ "" + ], + "Multiple": [ + "" + ], + "Allows you to select multiple values": [ + "" + ], + "Yes": [ + "" ] } } diff --git a/src/languages/lzb-select-dynamic.pot b/src/languages/lzb-select-dynamic.pot index bc00a16..107af1c 100644 --- a/src/languages/lzb-select-dynamic.pot +++ b/src/languages/lzb-select-dynamic.pot @@ -9,7 +9,7 @@ msgstr "" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" -"POT-Creation-Date: 2022-02-07T10:35:37+00:00\n" +"POT-Creation-Date: 2022-07-04T09:07:39+00:00\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "X-Generator: WP-CLI 2.5.0\n" "X-Domain: @@text_domain\n" @@ -50,54 +50,66 @@ msgstr "" msgid "Loading.." msgstr "" -#: assets/js/select-dynamic.jsx:51 +#: assets/js/select-dynamic.jsx:53 msgid "Entity Type" msgstr "" -#: assets/js/select-dynamic.jsx:52 +#: assets/js/select-dynamic.jsx:54 msgid "Allows you to select the entity type to render as select options" msgstr "" -#: assets/js/select-dynamic.jsx:57 +#: assets/js/select-dynamic.jsx:59 msgid "Post Type" msgstr "" -#: assets/js/select-dynamic.jsx:60 +#: assets/js/select-dynamic.jsx:62 msgid "Taxonomy Type" msgstr "" -#: assets/js/select-dynamic.jsx:63 +#: assets/js/select-dynamic.jsx:65 msgid "Post" msgstr "" -#: assets/js/select-dynamic.jsx:66 +#: assets/js/select-dynamic.jsx:68 msgid "Page" msgstr "" -#: assets/js/select-dynamic.jsx:69 +#: assets/js/select-dynamic.jsx:71 msgid "Taxonomy" msgstr "" -#: assets/js/select-dynamic.jsx:80 +#: assets/js/select-dynamic.jsx:82 msgid "Post type" msgstr "" -#: assets/js/select-dynamic.jsx:81 +#: assets/js/select-dynamic.jsx:83 msgid "Allows you to select a custom post type. Defaults to \"Post\" if nothing is selected" msgstr "" -#: assets/js/select-dynamic.jsx:93 +#: assets/js/select-dynamic.jsx:95 msgid "Taxonomy type" msgstr "" -#: assets/js/select-dynamic.jsx:94 +#: assets/js/select-dynamic.jsx:96 msgid "Allows you to select a taxonomy type. Defaults to \"Category\" if nothing is selected" msgstr "" -#: assets/js/select-dynamic.jsx:106 +#: assets/js/select-dynamic.jsx:108 msgid "Parent entity" msgstr "" -#: assets/js/select-dynamic.jsx:107 +#: assets/js/select-dynamic.jsx:109 msgid "Allows you to restrict the select options to a certain parent entity, otherwise leave blank" msgstr "" + +#: assets/js/select-dynamic.jsx:125 +msgid "Multiple" +msgstr "" + +#: assets/js/select-dynamic.jsx:126 +msgid "Allows you to select multiple values" +msgstr "" + +#: assets/js/select-dynamic.jsx:129 +msgid "Yes" +msgstr "" diff --git a/src/readme.md b/src/readme.md index 0337c9f..6c755ea 100644 --- a/src/readme.md +++ b/src/readme.md @@ -3,7 +3,7 @@ * Contributors: janwidmer * Tags: lazy-blocks, lazy blocks, custom control, select dynamic * Requires at least: 5.8 -* Tested up to: 5.9.0 +* Tested up to: 6.1.0 * Requires PHP: 7.2 * Stable tag: @@plugin_version * License: GPLv2 or later @@ -29,6 +29,12 @@ the Wordpress Author to choose either wordpress posts, pages or categories. ### Restrictions +#### Select Multiple Restrictions + +* The option "Select Multiple" is only available for the entity type `Post` / `Pages` / `Taxonomy` + +#### Max Item Restriction + The custom control is using the `getEntityRecords` method. * This method can load a maximum of 100 entities and render them as dropdown options @@ -67,6 +73,11 @@ You can use the custom control exactly how you use normal controls for lazy bloc ## Changelog += 2.4.0 = + +* Add option to select multiple items for Entity Types `post` | `page` | `taxonomy` +* changed compatibility to WP version to 6.1.0 + = 2.2.1 = * Fix problem with missing custom post types for entity type `post-type`