Skip to content

Commit

Permalink
changed meta block attributes to use meta directly with useEntityProp…
Browse files Browse the repository at this point in the history
… (issue #244)
  • Loading branch information
nk-o committed Jun 20, 2023
1 parent df8b273 commit 23fdf97
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 11 deletions.
17 changes: 14 additions & 3 deletions assets/components/render-controls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export default class RenderControls extends Component {
}

onControlChange(val, control, childIndex) {
const { lazyBlockData, attributes, setAttributes } = this.props;
const { lazyBlockData, attributes, setAttributes, meta, setMeta } =
this.props;
let { name } = control;

// prepare child items.
Expand All @@ -54,6 +55,7 @@ export default class RenderControls extends Component {
) {
const childs = getControlValue(
attributes,
meta,
lazyBlockData,
lazyBlockData.controls[control.child_of]
);
Expand Down Expand Up @@ -81,7 +83,14 @@ export default class RenderControls extends Component {
childIndex
);

setAttributes({ [name]: val });
// Save post Meta.
if (control.save_in_meta === 'true') {
setMeta({ ...meta, [control.save_in_meta_name || name]: val });

// Save block attribute.
} else {
setAttributes({ [name]: val });
}
}

/**
Expand Down Expand Up @@ -174,7 +183,8 @@ export default class RenderControls extends Component {
* @return {object|boolean} react control.
*/
renderControl(controlData, placement, uniqueId, childIndex = false) {
const { lazyBlockData, isLazyBlockSelected, attributes } = this.props;
const { lazyBlockData, isLazyBlockSelected, attributes, meta } =
this.props;
let result = false;

let placementCheck =
Expand Down Expand Up @@ -249,6 +259,7 @@ export default class RenderControls extends Component {
) =>
getControlValue(
attributes,
meta,
lazyBlockData,
optionalControl,
optionalChildIndex
Expand Down
23 changes: 22 additions & 1 deletion assets/editor/blocks/main/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { __ } from '@wordpress/i18n';
import { useRef, useEffect } from '@wordpress/element';
import { ToolbarButton } from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { useEntityProp } from '@wordpress/core-data';
import { useThrottle } from '@wordpress/compose';
import {
InspectorControls,
Expand Down Expand Up @@ -41,17 +42,21 @@ export default function BlockEdit(props) {
const isFirstLoad = useRef(true);
const isMounted = useRef(true);

const { innerBlockSelected } = useSelect(
const { innerBlockSelected, postType } = useSelect(
(select) => {
const { hasSelectedInnerBlock } = select('core/block-editor');
const { getCurrentPostType } = select('core/editor');

return {
innerBlockSelected: hasSelectedInnerBlock(clientId, true),
postType: getCurrentPostType && getCurrentPostType(),
};
},
[clientId]
);

const [meta, setMeta] = useEntityProp('postType', postType, 'meta');

const isLazyBlockSelected = isSelected || innerBlockSelected;

const {
Expand Down Expand Up @@ -98,6 +103,7 @@ export default function BlockEdit(props) {
if (lazyBlockData.controls[control.child_of]) {
const childs = getControlValue(
attributes,
meta,
lazyBlockData,
lazyBlockData.controls[control.child_of]
);
Expand All @@ -106,6 +112,7 @@ export default function BlockEdit(props) {
childs.forEach((childData, childIndex) => {
const val = getControlValue(
attributes,
meta,
lazyBlockData,
control,
childIndex
Expand All @@ -122,6 +129,7 @@ export default function BlockEdit(props) {
} else {
const val = getControlValue(
attributes,
meta,
lazyBlockData,
control
);
Expand Down Expand Up @@ -181,6 +189,7 @@ export default function BlockEdit(props) {
if (!lazyBlockData.controls[k].child_of) {
attsForRender[lazyBlockData.controls[k].name] = getControlValue(
attributes,
meta,
lazyBlockData,
lazyBlockData.controls[k]
);
Expand Down Expand Up @@ -240,6 +249,12 @@ export default function BlockEdit(props) {
<RenderControls
placement="inspector"
isLazyBlockSelected={isLazyBlockSelected}
meta={meta}
setMeta={(...args) => {
if (postType) {
setMeta(...args);
}
}}
{...props}
/>
</div>
Expand Down Expand Up @@ -277,6 +292,12 @@ export default function BlockEdit(props) {
<RenderControls
placement="content"
isLazyBlockSelected={isLazyBlockSelected}
meta={meta}
setMeta={(...args) => {
if (postType) {
setMeta(...args);
}
}}
{...props}
/>
</div>
Expand Down
7 changes: 7 additions & 0 deletions assets/utils/get-control-value/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ import { applyFilters } from '@wordpress/hooks';

export default function getControlValue(
attributes,
meta = {},
lazyBlockData,
control,
childIndex
) {
let result = attributes[control.name];

// Prepare Meta control value.
if (control.save_in_meta === 'true') {
result = meta[control.save_in_meta_name || control.name];
}

// Prepare child items.
if (
control.child_of &&
Expand All @@ -19,6 +25,7 @@ export default function getControlValue(
) {
const childs = getControlValue(
attributes,
meta,
lazyBlockData,
lazyBlockData.controls[control.child_of]
);
Expand Down
59 changes: 52 additions & 7 deletions classes/class-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -1218,7 +1218,6 @@ public function register_block() {

/**
* Prepare attributes.
* The same function placed in block JSX file.
*
* @param array $controls - controls list.
* @param string|boolean $child_of - childOf control name.
Expand All @@ -1236,16 +1235,17 @@ public function prepare_block_attributes( $controls, $child_of = '', $block = nu
$control['child_of'] === $child_of &&
$control['name']
) {
// Prevent registering Meta controls, as we don't save the meta inside the block.
// Meta controls prepared inside `prepare_block_meta_attributes` method.
if ( isset( $control['save_in_meta'] ) && 'true' === $control['save_in_meta'] ) {
continue;
}

$attribute_data = array(
'type' => 'string',
'default' => isset( $control['default'] ) ? $control['default'] : null,
);

if ( isset( $control['save_in_meta'] ) && 'true' === $control['save_in_meta'] ) {
$attribute_data['source'] = 'meta';
$attribute_data['meta'] = isset( $control['save_in_meta_name'] ) && $control['save_in_meta_name'] ? $control['save_in_meta_name'] : $control['name'];
}

// get attribute type from control data.
if ( isset( $control['type'] ) && isset( $all_controls[ $control['type'] ] ) ) {
$attribute_data['type'] = $all_controls[ $control['type'] ]['type'];
Expand Down Expand Up @@ -1300,6 +1300,50 @@ public function prepare_block_attributes( $controls, $child_of = '', $block = nu
return $attributes;
}

/**
* Prepare meta attributes.
*
* @param array $controls - controls list.
* @param string|boolean $child_of - childOf control name.
* @param array $block - block data.
*
* @return array.
*/
public function prepare_block_meta_attributes( $controls, $child_of = '', $block = null ) {
$all_controls = lazyblocks()->controls()->get_controls();
$attributes = array();

foreach ( $controls as $k => $control ) {
if (
isset( $control['child_of'] ) &&
$control['child_of'] === $child_of &&
$control['name'] &&
isset( $control['save_in_meta'] ) &&
'true' === $control['save_in_meta']
) {
$attribute_data = array(
'source' => 'meta',
'type' => 'string',
'meta' => isset( $control['save_in_meta_name'] ) && $control['save_in_meta_name'] ? $control['save_in_meta_name'] : $control['name'],
'default' => isset( $control['default'] ) ? $control['default'] : null,
);

// get attribute type from control data.
if ( isset( $control['type'] ) && isset( $all_controls[ $control['type'] ] ) ) {
$attribute_data['type'] = $all_controls[ $control['type'] ]['type'];

if ( 'number' === $attribute_data['type'] && null !== $attribute_data['default'] ) {
$attribute_data['default'] = (float) $attribute_data['default'];
}
}

$attributes[ $control['name'] ] = apply_filters( 'lzb/prepare_block_attribute', $attribute_data, $control, $controls, $k, $block );
}
}

return $attributes;
}

/**
* Eval custom user code and return as string.
*
Expand Down Expand Up @@ -1356,7 +1400,8 @@ public function register_block_render() {
register_block_type( $block['slug'], $data );

// Register meta.
foreach ( $attributes as $attribute ) {
$meta_attributes = $this->prepare_block_meta_attributes( $block['controls'], '', $block );
foreach ( $meta_attributes as $attribute ) {
if ( isset( $attribute['meta'] ) && $attribute['meta'] ) {
register_meta(
'post',
Expand Down

0 comments on commit 23fdf97

Please sign in to comment.