Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mejta committed Oct 25, 2024
1 parent 2919003 commit 1990190
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 12 deletions.
16 changes: 11 additions & 5 deletions assets/fields/MultiCheckbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@ function MultiCheckbox ({
id,
htmlId,
onChange,
value = {},
value = [],
options,
attributes = {},
className,
}) {
const handleChange = useCallback(optionValue => event => {
const nextValue = { ...value };
nextValue[optionValue] = event.target.checked;
onChange(nextValue);
const nextValue = Array.isArray(value) ? [ ...value ] : [];

if (event.target.checked) {
nextValue.push(optionValue);
} else {
nextValue.splice(nextValue.indexOf(optionValue), 1);
}

onChange(nextValue.filter((value, index, array) => array.indexOf(value) === index));
}, [onChange, value]);

return (
Expand All @@ -26,7 +32,7 @@ function MultiCheckbox ({
type="checkbox"
id={`${htmlId}-${option.value}`}
onChange={handleChange(option.value)}
checked={value[option.value] || false}
checked={Array.isArray(value) ? value.includes(option.value) : false}
{...attributes}
/>
<label
Expand Down
16 changes: 13 additions & 3 deletions assets/fields/MultiToggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,21 @@ function MultiToggle ({
id,
htmlId,
onChange,
value = {},
value = [],
options,
className,
}) {
const handleChange = useCallback(optionValue => checked => onChange({ ...value, [optionValue]: checked }), [onChange, value]);
const handleChange = useCallback(optionValue => checked => {
const nextValue = Array.isArray(value) ? [ ...value ] : [];

if (checked) {
nextValue.push(optionValue);
} else {
nextValue.splice(nextValue.indexOf(optionValue), 1);
}

onChange(nextValue.filter((value, index, array) => array.indexOf(value) === index));
}, [onChange, value]);

return (
<div className={clsx('wpifycf-field-multi-toggle', `wpifycf-field-multi-toggle--${id}`, className)}>
Expand All @@ -21,7 +31,7 @@ function MultiToggle ({
<ToggleControl
id={`${htmlId}-${option.value}`}
onChange={handleChange(option.value)}
checked={value[option.value] || false}
checked={Array.isArray(value) ? value.includes(option.value) : false}
label={option.label}
/>
</div>
Expand Down
2 changes: 1 addition & 1 deletion build/wpify-custom-fields.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'react-dom', 'react-jsx-runtime', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-core-data', 'wp-data', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-primitives', 'wp-server-side-render', 'wp-url'), 'version' => '4b7791bfc362c2778004');
<?php return array('dependencies' => array('react', 'react-dom', 'react-jsx-runtime', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-core-data', 'wp-data', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-primitives', 'wp-server-side-render', 'wp-url'), 'version' => 'aa9310bce80921fadc62');
2 changes: 1 addition & 1 deletion build/wpify-custom-fields.js

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions src/CustomFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,15 @@ public function sanitize_item_value( array $item ): Closure {
$sanitized_value = esc_url( $value );
} elseif ( 'wysiwyg' === $item['type'] ) {
$sanitized_value = wp_kses_post( $value );
} elseif ( in_array( $item['type'], array( 'multi_checkbox', 'multi_toggle' ), true ) ) {
$value = is_string( $value ) ? json_decode( $value, true ) : (array) $value;
$sanitized_value = array();

if ( is_array( $value ) ) {
foreach ( $value as $sub_value ) {
$sanitized_value[] = sanitize_text_field( $sub_value );
}
}
} elseif ( str_starts_with( $item['type'], 'multi_' ) ) {
$single_type = substr( $item['type'], strlen( 'multi_' ) );
$value = is_string( $value ) ? json_decode( $value, true ) : (array) $value;
Expand Down Expand Up @@ -436,8 +445,6 @@ public function get_wp_type( array $item ): string {
'group',
'link',
'mapycz',
'multi_toggle',
'multi_checkbox',
),
true
) ) {
Expand Down
16 changes: 16 additions & 0 deletions src/Integrations/BaseIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ protected function normalize_item( array $item, string $global_id ): array {
$item['default'] = $this->custom_fields->get_default_value( $item );
}

/* Compatibility with WPify Woo */
$type_aliases = array(
'multiswitch' => 'multi_toggle',
'switch' => 'toggle',
'multiselect' => 'multi_select',
'colorpicker' => 'color',
'gallery' => 'multi_attachment',
'repeater' => 'multi_group',
);

foreach ( $type_aliases as $alias => $correct ) {
if ( $item['type'] === $alias ) {
$item['type'] = $correct;
}
}

if ( isset( $item['items'] ) ) {
$item['items'] = $this->normalize_items( $item['items'], $item['global_id'] );
}
Expand Down

0 comments on commit 1990190

Please sign in to comment.