Skip to content

Commit

Permalink
add setting conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
picocodes committed Oct 15, 2023
1 parent 060e5b8 commit 3a3a855
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 10 deletions.
2 changes: 1 addition & 1 deletion includes/assets/js/dist/edit-automation-rule.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-components', 'wp-dom-ready', 'wp-element', 'wp-i18n', 'wp-primitives'), 'version' => '4ff4105413d9685ef27a');
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-components', 'wp-dom-ready', 'wp-element', 'wp-i18n', 'wp-primitives'), 'version' => 'a0ac521b682b1ab865a6');
2 changes: 1 addition & 1 deletion includes/assets/js/dist/edit-automation-rule.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion includes/assets/js/dist/edit-email-campaign.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('wp-api-fetch', 'wp-components', 'wp-dom-ready', 'wp-element', 'wp-i18n', 'wp-primitives'), 'version' => 'd37d8913a08a23b78d18');
<?php return array('dependencies' => array('wp-api-fetch', 'wp-components', 'wp-dom-ready', 'wp-element', 'wp-i18n', 'wp-primitives'), 'version' => '3ccbe67f434868dba4ef');
2 changes: 1 addition & 1 deletion includes/assets/js/dist/edit-email-campaign.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion includes/assets/js/dist/table.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('lodash', 'react', 'wp-components', 'wp-data', 'wp-data-controls', 'wp-date', 'wp-dom-ready', 'wp-element', 'wp-i18n', 'wp-polyfill', 'wp-primitives', 'wp-url'), 'version' => '0424abb20b2409b11934');
<?php return array('dependencies' => array('lodash', 'react', 'wp-components', 'wp-data', 'wp-data-controls', 'wp-date', 'wp-dom-ready', 'wp-element', 'wp-i18n', 'wp-polyfill', 'wp-primitives', 'wp-url'), 'version' => 'c352c8dec575935b3aa9');
2 changes: 1 addition & 1 deletion includes/assets/js/dist/table.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion includes/assets/js/dist/welcome-wizard.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('wp-api-fetch', 'wp-components', 'wp-dom-ready', 'wp-element', 'wp-i18n', 'wp-primitives'), 'version' => '81d371460739a31a5e17');
<?php return array('dependencies' => array('wp-api-fetch', 'wp-components', 'wp-dom-ready', 'wp-element', 'wp-i18n', 'wp-primitives'), 'version' => '8a8636387d44461c0696');
2 changes: 1 addition & 1 deletion includes/assets/js/dist/welcome-wizard.js

Large diffs are not rendered by default.

28 changes: 26 additions & 2 deletions includes/assets/js/src/components/setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ import {
} from '@wordpress/components';
import { next } from '@wordpress/icons';
import { __, sprintf } from '@wordpress/i18n';
import { useCallback, useState, useMemo } from '@wordpress/element';
import { useCallback, useMemo } from '@wordpress/element';

/**
* Local dependancies.
*/
import ConditionalLogicEditor from './conditional-logic-editor';
import { compare } from '../utils/operators';

/**
* Input types.
Expand Down Expand Up @@ -124,7 +125,7 @@ function useMergeTags(availableSmartTags, onMergeTagClick) {

return groups;
}, [availableSmartTags]);
console.log(groups);

const totalGroups = Object.keys(groups).length;

// If we have merge tags, show the merge tags button.
Expand Down Expand Up @@ -407,6 +408,29 @@ export default function Setting({ settingKey, setting, availableSmartTags, prop,
}
}

// Key value conditions.
if ( Array.isArray( setting.conditions ) ) {

// Check if all conditions are met.
const conditionsMet = setting.conditions.every((condition) => {
const parts = condition.key.split( '.' );
const operator = condition.operator ? condition.operator : '==';
const compareAgainst = saved[ parts[0] ];

// If we have two parts, we're checking a nested setting.
if ( parts.length === 2 ) {
return compareAgainst && compareAgainst[ parts[1] ] && compare( condition.value, operator, compareAgainst[ parts[1] ] );
}

return compare( condition.value, operator, compareAgainst );
});

// If conditions are not met, return null.
if ( ! conditionsMet ) {
return null;
}
}

// Abort early if condition is not met.
if ( setting.condition && ! setting.condition( saved ) ) {
return null;
Expand Down
16 changes: 16 additions & 0 deletions includes/assets/js/src/utils/operators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const operators = {
'==': (a, b) => a == b,
'===': (a, b) => a === b,
'!=': (a, b) => a != b,
'!==': (a, b) => a !== b,
'>': (a, b) => a > b,
'>=': (a, b) => a >= b,
'<': (a, b) => a < b,
'<=': (a, b) => a <= b,
'includes': (a, b) => a.includes(b),
'!includes': (a, b) => ! a.includes(b),
'empty': (a) => ! a,
'!empty': (a) => a,
};

export const compare = (a, operator, b) => operators[operator] ? operators[operator](a, b) : false;

0 comments on commit 3a3a855

Please sign in to comment.