Skip to content

Commit

Permalink
draft object types implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
picocodes committed Oct 15, 2023
1 parent f3927bd commit 060e5b8
Show file tree
Hide file tree
Showing 22 changed files with 1,126 additions and 95 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' => '36828f4215041f4e3226');
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-components', 'wp-dom-ready', 'wp-element', 'wp-i18n', 'wp-primitives'), 'version' => '4ff4105413d9685ef27a');
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' => '58b806670ef6c49aa8af');
<?php return array('dependencies' => array('wp-api-fetch', 'wp-components', 'wp-dom-ready', 'wp-element', 'wp-i18n', 'wp-primitives'), 'version' => 'd37d8913a08a23b78d18');
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' => 'cef8e051fdccfe69bf69');
<?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');
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' => '5f3bc57636d0b31d01a3');
<?php return array('dependencies' => array('wp-api-fetch', 'wp-components', 'wp-dom-ready', 'wp-element', 'wp-i18n', 'wp-primitives'), 'version' => '81d371460739a31a5e17');
2 changes: 1 addition & 1 deletion includes/assets/js/dist/welcome-wizard.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions includes/assets/js/src/components/automation-rules/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ export function getAvailableSmartTags( smartTags, trigger_settings ) {
placeholder: smartTag.placeholder ? smartTag.placeholder : '',
conditional_logic: smartTag.conditional_logic ? smartTag.conditional_logic : false,
options: smartTag.options ? smartTag.options : [],
icon: smartTag.icon,
group: smartTag.group,
} )
} );

Expand Down
186 changes: 107 additions & 79 deletions includes/assets/js/src/components/setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
CheckboxControl,
FormTokenField,
DropdownMenu,
MenuGroup,
MenuItem,
Tip,
Button,
Flex,
Expand Down Expand Up @@ -105,68 +107,87 @@ const keyValueRepeaterFields = [
function useMergeTags(availableSmartTags, onMergeTagClick) {

// Dropdown menu controls.
const controls = useMemo(() => {
const groups = useMemo(() => {

if ( ! Array.isArray(availableSmartTags) ) {
return [];
return {};
}

// Returns the merge tag value to use.
const theValue = ( mergeTag ) => {

if ( mergeTag.example ) {
return mergeTag.example;
}

let defaultValue = "default value";

if ( mergeTag.replacement ) {
defaultValue = mergeTag.replacement;
}

if ( mergeTag.default ) {
defaultValue = mergeTag.default;
}

if ( ! defaultValue ) {
return `${mergeTag.smart_tag}`;
}

return `${mergeTag.smart_tag} default="${defaultValue}"`;
}
const groups = {};

return availableSmartTags.map((smartTag) => {
return {
title: smartTag.label,
icon: next,
onClick: () => {

if ( onMergeTagClick ) {
onMergeTagClick(`[[${theValue(smartTag)}]]`);
}
},
};
availableSmartTags.forEach((smartTag) => {
const group = smartTag.group ? smartTag.group : __( 'General', 'newsletter-optin-box' );
groups[group] = groups[group] ? groups[group] : [];

groups[group].push( smartTag );
});
}, [availableSmartTags, onMergeTagClick]);

return groups;
}, [availableSmartTags]);
console.log(groups);
const totalGroups = Object.keys(groups).length;

// If we have merge tags, show the merge tags button.
let inserter = null;

if ( controls.length > 0 ) {
if ( totalGroups > 0 ) {

inserter = (
<DropdownMenu
icon="shortcode"
controls={ controls }
label={__( 'Insert merge tag', 'newsletter-optin-box' )}
showTooltip
/>
>
{ ( { onClose } ) => (
<>
{ Object.keys(groups).map((group, index) => (
<MenuGroup label={ totalGroups > 1 ? group : undefined} key={index}>
{ groups[group].map((item) => (
<MenuItem
icon={ item.icon || next }
iconPosition="left"
onClick={ () => {
if ( onMergeTagClick ) {
onMergeTagClick(`[[${getMergeTagValue(item)}]]`);
}

onClose();
}}
key={ item.smart_tag }
>
{ item.label }
</MenuItem>
)) }
</MenuGroup>
)) }
</>
) }
</DropdownMenu>
);
}

return inserter;
}

/**
* Returns a merge tag's value.
*
* @param {Object} smartTag The smart tag.
* @return {Array}
*/
function getMergeTagValue(smartTag) {

if ( smartTag.example ) {
return smartTag.example;
}

if ( ! smartTag.default ) {
return `${smartTag.smart_tag}`;
}

return `${smartTag.smart_tag} default="${smartTag.default}"`;
}

/**
* Displays a key value repeater setting.
*
Expand All @@ -178,21 +199,6 @@ function useMergeTags(availableSmartTags, onMergeTagClick) {
*/
function KeyValueRepeater({ setting, availableSmartTags, value, onChange, ...attributes }) {

const [currentField, setCurrentField] = useState(false);

// On add merge tag...
const onMergeTagClick = useCallback((mergeTag) => {

if ( Array.isArray( currentField ) ) {
value = [...value];
value[currentField[0]][currentField[1]] += mergeTag;
onChange(value);
}
}, [currentField, value, onChange]);

// Merge tags.
const [suffix] = useMergeTags( availableSmartTags, onMergeTagClick );

// The base props.
const { baseControlProps, controlProps } = useBaseControlProps( attributes );

Expand All @@ -201,38 +207,17 @@ function KeyValueRepeater({ setting, availableSmartTags, value, onChange, ...att
value = [];
}

// Displays a single field in the repeater.
const Field = useCallback(({ item, field, index, onChange }) => {
return (
<FlexBlock>
<InputControl
label={field.label}
type={field.type}
value={item[field.id] === undefined ? '' : item[field.id]}
placeholder={sprintf( __( 'Enter %s', 'noptin-addons-pack' ), field.label )}
className="noptin-component__field noptin-condition-field"
suffix={suffix}
onChange={onChange}
onFocus={() => { setCurrentField([index, field.id])}}
isPressEnterToChange
__nextHasNoMarginBottom
__next36pxDefaultSize
/>
</FlexBlock>
);
}, [suffix]);

// Displays a single Item.
const Item = useCallback(({ item, index }) => {
return (
<Flex className="noptin-repeater-item" wrap>

{keyValueRepeaterFields.map((field, fieldIndex) => (
<Field
<KeyValueRepeaterField
key={fieldIndex}
index={index}
item={item}
availableSmartTags={ availableSmartTags }
field={field}
value={item[field.id] === undefined ? '' : item[field.id]}
onChange={(newValue) => {
const newItems = [...value];
newItems[index][field.id] = newValue;
Expand All @@ -253,6 +238,7 @@ function KeyValueRepeater({ setting, availableSmartTags, value, onChange, ...att
newValue.splice(index, 1);
onChange(newValue);
}}
isDestructive
/>
</FlexItem>
</Flex>
Expand Down Expand Up @@ -281,6 +267,48 @@ function KeyValueRepeater({ setting, availableSmartTags, value, onChange, ...att
);
}

/**
* Displays a key value repeater setting field.
*
* @param {Object} props
* @param {Function} props.onChange The on change handler
* @param {String} props.value The current value.
* @param {Object} props.field The field object.
* @param {Array} props.availableSmartTags The available smart tags.
* @return {JSX.Element}
*/
function KeyValueRepeaterField({ field, availableSmartTags, value, onChange }) {

// On add merge tag...
const onMergeTagClick = useCallback((mergeTag) => {

// Add the merge tag to the value.
if ( onChange ) {
onChange(value ? `${value} ${mergeTag}`.trim() : mergeTag);
}
}, [value, onChange]);

// Merge tags.
const suffix = useMergeTags( availableSmartTags, onMergeTagClick );

return (
<FlexBlock>
<InputControl
label={field.label}
type={field.type}
value={value}
placeholder={sprintf( __( 'Enter %s', 'noptin-addons-pack' ), field.label )}
className="noptin-component__field noptin-condition-field"
suffix={suffix}
onChange={onChange}
isPressEnterToChange
__nextHasNoMarginBottom
__next36pxDefaultSize
/>
</FlexBlock>
);
}

/**
* Displays a multi-checkbox setting.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function __construct( $trigger, $subject, $extra_args = array() ) {

$values = array_merge( $extra_args, $trigger->prepare_known_smart_tags( $subject ) );

if ( ! empty( $extra_args['extra_args'] ) ) {
$values = array_merge( $values, $extra_args['extra_args'] );
}

foreach ( $trigger->get_known_smart_tags() as $merge_tag => $tag ) {

if ( isset( $values[ $merge_tag ] ) && 'subject' !== $merge_tag ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ public function get_known_smart_tags_for_js() {
foreach ( $smart_tags as $key => $value ) {
$prepared[ $key ] = $value;

if ( empty( $prepared[ $key ]['group'] ) ) {
$prepared[ $key ]['group'] = __( 'General', 'newsletter-optin-box' );
}

// Remove callback.
unset( $prepared[ $key ]['callback'] );
}
Expand All @@ -140,7 +144,7 @@ public function get_known_smart_tags_for_js() {
* @return array
*/
public function get_known_smart_tags() {
/** @var WP_Locale $wp_locale */
/** @var \WP_Locale $wp_locale */
global $wp_locale;

$smart_tags = array(
Expand Down Expand Up @@ -212,6 +216,7 @@ public function get_known_smart_tags() {
'yes' => __( 'Logged in', 'newsletter-optin-box' ),
'no' => __( 'Logged out', 'newsletter-optin-box' ),
),
'group' => __( 'User', 'newsletter-optin-box' ),
);
} else {
$smart_tags = array_replace(
Expand All @@ -221,54 +226,64 @@ public function get_known_smart_tags() {
'user_id' => array(
'description' => __( 'User ID', 'newsletter-optin-box' ),
'conditional_logic' => 'number',
'group' => __( 'User', 'newsletter-optin-box' ),
),

'user_role' => array(
'description' => __( 'User Role', 'newsletter-optin-box' ),
'conditional_logic' => 'string',
'options' => wp_roles()->get_names(),
'group' => __( 'User', 'newsletter-optin-box' ),
),

'user_locale' => array(
'description' => __( 'User Locale', 'newsletter-optin-box' ),
'conditional_logic' => 'string',
'example' => 'user_locale default="en_US"',
'options' => noptin_get_available_languages(),
'group' => __( 'User', 'newsletter-optin-box' ),
),

'email' => array(
'description' => __( 'Email Address', 'newsletter-optin-box' ),
'conditional_logic' => 'string',
'group' => __( 'User', 'newsletter-optin-box' ),
),

'name' => array(
'description' => __( 'Display Name', 'newsletter-optin-box' ),
'conditional_logic' => 'string',
'group' => __( 'User', 'newsletter-optin-box' ),
),

'first_name' => array(
'description' => __( 'First Name', 'newsletter-optin-box' ),
'conditional_logic' => 'string',
'group' => __( 'User', 'newsletter-optin-box' ),
),

'last_name' => array(
'description' => __( 'Last Name', 'newsletter-optin-box' ),
'conditional_logic' => 'string',
'group' => __( 'User', 'newsletter-optin-box' ),
),

'user_login' => array(
'description' => __( 'Login Name', 'newsletter-optin-box' ),
'conditional_logic' => 'string',
'group' => __( 'User', 'newsletter-optin-box' ),
),

'user_url' => array(
'description' => __( 'User URL', 'newsletter-optin-box' ),
'conditional_logic' => 'string',
'group' => __( 'User', 'newsletter-optin-box' ),
),

'user_bio' => array(
'description' => __( 'User Bio', 'newsletter-optin-box' ),
'conditional_logic' => 'string',
'group' => __( 'User', 'newsletter-optin-box' ),
),
)
);
Expand Down Expand Up @@ -594,6 +609,8 @@ public function trigger( $subject, $args ) {

$args = $this->prepare_trigger_args( $subject, $args );

$GLOBALS['noptin_current_trigger_args'] = $args;

foreach ( $this->get_rules() as $rule ) {

// Retrieve the action.
Expand Down
Loading

0 comments on commit 060e5b8

Please sign in to comment.