Skip to content

Commit

Permalink
enhance to offer a multiple option for post, page and taxonomy
Browse files Browse the repository at this point in the history
  • Loading branch information
janwidmer-work committed Jul 4, 2022
1 parent 9f85fa6 commit d3513b1
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .node-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16.13.1
16.15.1
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/janwidmer>",
Expand Down
11 changes: 7 additions & 4 deletions src/assets/js/select-dynamic-control.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down Expand Up @@ -71,6 +71,7 @@ class SelectDynamicControl extends Component {
options={ choices }
help={ help }
value={ value }
multiple={ multiple }
className="lzb-gutenberg-select"
onChange={(val) => {
onChange(val);
Expand All @@ -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;
}

Expand Down
17 changes: 17 additions & 0 deletions src/assets/js/select-dynamic.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {
PanelBody,
BaseControl,
RadioControl,
CheckboxControl,
} = wp.components;

/**
Expand All @@ -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}
/>
Expand Down Expand Up @@ -115,7 +117,22 @@ addFilter('lzb.constructor.control.select_dynamic.settings', 'lzb.constructor',
/>
</BaseControl>
)}

</PanelBody>
{(props.data.entity_type && (props.data.entity_type === 'post' || props.data.entity_type === 'page' || props.data.entity_type === 'taxonomy')) && (
<PanelBody>
<BaseControl
label={ __( 'Multiple', '@@text_domain' ) }
help={ __( 'Allows you to select multiple values', '@@text_domain' ) }
>
<CheckboxControl
label={ __( 'Yes', '@@text_domain' ) }
checked={ 'true' === data.multiple }
onChange={ ( value ) => updateData( { multiple: value ? 'true' : 'false' } ) }
/>
</BaseControl>
</PanelBody>
)}
</Fragment>
);
});
32 changes: 31 additions & 1 deletion src/controls/select-dynamic.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions src/languages/lzb-select-dynamic.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
""
]
}
}
Expand Down
40 changes: 26 additions & 14 deletions src/languages/lzb-select-dynamic.pot
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 ""
13 changes: 12 additions & 1 deletion src/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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`
Expand Down

0 comments on commit d3513b1

Please sign in to comment.