Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOM test class changes #8

Merged
merged 4 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 98 additions & 10 deletions acf-fields/split-test-post.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"title": "Post Title Test",
"dom": "DOM Test"
},
"default_value": false,
"default_value": "dom",
"return_format": "value",
"multiple": 0,
"allow_null": 0,
Expand Down Expand Up @@ -168,15 +168,7 @@
"type": "repeater",
"instructions": "",
"required": 0,
"conditional_logic": [
[
{
"field": "field_668fec3b09224",
"operator": "==",
"value": "dom"
}
]
],
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
Expand Down Expand Up @@ -254,6 +246,102 @@
}
],
"parent_repeater": "field_66982f55bb940"
},
{
"key": "field_6740b0595dbdb",
"label": "Change classes",
"name": "classes",
"aria-label": "",
"type": "repeater",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"layout": "block",
"pagination": 0,
"min": 0,
"max": 0,
"collapsed": "",
"button_label": "Add Change",
"rows_per_page": 20,
"sub_fields": [
{
"key": "field_6740b0795dbdc",
"label": "Selector",
"name": "selector",
"aria-label": "",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"maxlength": "",
"allow_in_bindings": 0,
"placeholder": "",
"prepend": "",
"append": "",
"parent_repeater": "field_6740b0595dbdb"
},
{
"key": "field_6740b0995dbdd",
"label": "Change",
"name": "change",
"aria-label": "",
"type": "radio",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"choices": {
"add": "Add a class",
"remove": "Remove a class"
},
"default_value": "add",
"return_format": "value",
"allow_null": 0,
"other_choice": 0,
"allow_in_bindings": 0,
"layout": "vertical",
"save_other_choice": 0,
"parent_repeater": "field_6740b0595dbdb"
},
{
"key": "field_6740b0c55dbde",
"label": "Class",
"name": "class",
"aria-label": "",
"type": "text",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"maxlength": "",
"allow_in_bindings": 0,
"placeholder": "",
"prepend": "",
"append": "",
"parent_repeater": "field_6740b0595dbdb"
}
],
"parent_repeater": "field_66982f55bb940"
}
]
},
Expand Down
2 changes: 0 additions & 2 deletions bin/build
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ if [ ! -f .env ] ; then
echo "What is your ACF Pro key?"
read -p "> " ACF_PRO_KEY
echo >> .env
echo "# This is for the ACF Pro installer plugin" >> .env
echo "# See: https://github.com/dphiffer/acf-pro-installer" >> .env
echo "ACF_PRO_KEY=$ACF_PRO_KEY" >> .env
echo
echo "Thank you!"
Expand Down
11 changes: 10 additions & 1 deletion src/DomTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,16 @@ function show_results($post) {
}

foreach ($_variants as $index => $variant) {
$_variants[$index]['description'] = count($variant['content']) . ' content changes';
// Describe content changes
$plural = (! empty($variant['content']) && count($variant['content']) > 1) ? 's' : '';
$description = empty($variant['content']) ? '' : count($variant['content']) . ' content change' . $plural;

// Describe class changes
$separator = empty($description) ? '' : ', ';
$plural = (! empty($variant['classes']) && count($variant['classes']) > 1) ? 's' : '';
$description .= empty($variant['classes']) ? '' : $separator . count($variant['classes']) . ' class change' . $plural;

$_variants[$index]['description'] = $description;
}

$variants = [
Expand Down
15 changes: 14 additions & 1 deletion src/split-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,27 @@ export default function split_tests_init() {
for (let replacement of variant.content) {
let targets = document.querySelectorAll(replacement.selector);
for (let target of targets) {
if (replacement.search && target.innerText.trim() != replacement.search.trim()) {
if (replacement.search != '' && target.innerText.trim() != replacement.search.trim()) {
continue;
}
target.innerText = replacement.replace;
}
}
}

if (variant.classes) {
for (let change of variant.classes) {
let targets = document.querySelectorAll(change.selector);
for (let target of targets) {
if (change.change == 'add') {
target.classList.add(change.class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm missing something - wouldn't this code mean that the added/removed classes were always added/removed?

Copy link
Contributor Author

@dphiffer dphiffer Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well it'll happen every time a variant lands on the page with a class add/removal, but that will only happen some of the time. fwiw DomTests.php:76 is where that random selection happens.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it, thanks!

} else if (change.change == 'remove') {
target.classList.remove(change.class);
}
}
}
}

if (variant.conversion == 'click') {
const selector = variant.click_selector;
const content = variant.click_content;
Expand Down