Skip to content

Commit

Permalink
Change visibilityConditions.match_all from boolean to '0' or '1'
Browse files Browse the repository at this point in the history
This is for backwards compatibility. I want the same code to process
legacy and block based visibility.
  • Loading branch information
mreishus committed Aug 18, 2021
1 parent 7117dac commit 55a2ba7
Showing 1 changed file with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,9 @@ const VisibilityRule = props => {
};

const RuleSep = props => {
const { matchAll } = props;
const { isAnd } = props;
const { createElement } = wp.element;
if ( matchAll ) {
if ( isAnd ) {
return createElement( 'div', null, 'and' );
}
return createElement( 'div', null, 'or' );
Expand All @@ -452,12 +452,12 @@ const visibilityAdvancedControls = wp.compose.createHigherOrderComponent( BlockE

// Initialize props.visibilityConditions if none is sent
useEffect( () => {
if ( ! ( 'action' in visibilityConditions ) || ! ( 'matchAll' in visibilityConditions ) ) {
if ( ! ( 'action' in visibilityConditions ) || ! ( 'match_all' in visibilityConditions ) ) {
setAttributes( {
visibilityConditions: {
action: 'show',
rules: [],
matchAll: false,
match_all: '0', // boolean with either '0' or '1' strings for backwards compat
},
} );
}
Expand All @@ -467,7 +467,7 @@ const visibilityAdvancedControls = wp.compose.createHigherOrderComponent( BlockE
setAttributes( {
visibilityConditions: {
...visibilityConditions,
matchAll: ! visibilityConditions.matchAll,
match_all: visibilityConditions.match_all === '0' ? '1' : '0',
},
} );

Expand Down Expand Up @@ -560,12 +560,12 @@ const visibilityAdvancedControls = wp.compose.createHigherOrderComponent( BlockE
( acc, item ) =>
acc === null
? [ item ]
: [ ...acc, <RuleSep matchAll={ !! visibilityConditions.matchAll } />, item ],
: [ ...acc, <RuleSep isAnd={ visibilityConditions.match_all === '1' } />, item ],
null
) }
<ToggleControl
label={ __( 'match all', 'jetpack' ) }
checked={ !! visibilityConditions.matchAll }
checked={ visibilityConditions.match_all === '1' }
onChange={ toggleMatchAll }
/>
<Button onClick={ addNewRule }>{ __( 'Add New Rule' ) }</Button>
Expand Down Expand Up @@ -612,15 +612,15 @@ const visibilityAdvancedControls = wp.compose.createHigherOrderComponent( BlockE
: [
...acc,
createElement( RuleSep, {
matchAll: !! visibilityConditions.matchAll,
isAnd: visibilityConditions.match_all === '1',
} ),
item,
],
null
),
createElement( ToggleControl, {
label: __( 'match all', 'jetpack' ),
checked: !! visibilityConditions.matchAll,
checked: visibilityConditions.match_all === '1',
onChange: toggleMatchAll,
} ),
createElement( Button, { onClick: addNewRule }, __( 'Add New Rule' ) )
Expand Down

0 comments on commit 55a2ba7

Please sign in to comment.