Skip to content

Commit 2acfc24

Browse files
committed
Simplify js files
1 parent 383d026 commit 2acfc24

File tree

4 files changed

+69
-78
lines changed

4 files changed

+69
-78
lines changed

assets/src/js/_acf-experiments.ts

Lines changed: 0 additions & 67 deletions
This file was deleted.

assets/src/js/acf-experiments.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

assets/src/js/experiments/index.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Admin experiments.
3+
*
4+
* @package Secure Custom Fields
5+
* @since 6.4.3
6+
*/
7+
8+
// Define interfaces for our data structures
9+
interface ExperimentsData {
10+
[key: string]: boolean; // Just store enabled status
11+
}
12+
13+
// Define the experiments object structure
14+
interface ExperimentsObject {
15+
[key: string]: boolean | ((name: string) => boolean);
16+
isEnabled: (name: string) => boolean;
17+
}
18+
19+
// Define a more complete ACF interface
20+
interface ACF {
21+
experiments?: ExperimentsObject;
22+
[key: string]: any;
23+
}
24+
25+
// Declare global types
26+
declare global {
27+
interface Window {
28+
acf: ACF; // Ensure acf is available on window
29+
acfExperiments?: ExperimentsData; // Variable created by wp_localize_script
30+
}
31+
var acf: ACF;
32+
var acfExperiments: ExperimentsData | undefined; // Variable created by wp_localize_script
33+
}
34+
35+
// Create a module to avoid global scope augmentation issues
36+
export {};
37+
38+
(function() {
39+
// End the function if acf doesn't exist
40+
if (typeof acf !== 'object' || acf === null) {
41+
return;
42+
}
43+
44+
// Add experiments to acf object
45+
acf.experiments = {
46+
isEnabled: function(name: string): boolean {
47+
return this.hasOwnProperty(name) && this[name] === true;
48+
}
49+
};
50+
51+
// Initialize when document is ready
52+
document.addEventListener('DOMContentLoaded', function() {
53+
if (!acf.experiments) return;
54+
55+
// Check if acfExperiments exists (created by wp_localize_script)
56+
if (typeof acfExperiments === 'undefined') {
57+
return;
58+
}
59+
60+
// Use the experiments data from wp_localize_script
61+
if (acfExperiments && acfExperiments.data) {
62+
// Copy all experiment flags directly to the experiments object
63+
Object.assign(acf.experiments, acfExperiments.data);
64+
}
65+
});
66+
67+
})();
68+

webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const commonConfig = {
2222
'js/pro/acf-pro-input': './assets/src/js/pro/acf-pro-input.js',
2323
'js/pro/acf-pro-ui-options-page':
2424
'./assets/src/js/pro/acf-pro-ui-options-page.js',
25-
'js/acf-experiments': './assets/src/js/_acf-experiments.ts',
25+
'js/acf-experiments': './assets/src/js/experiments',
2626
// CSS files
2727
'css/acf-dark': './assets/src/sass/acf-dark.scss',
2828
'css/acf-field-group': './assets/src/sass/acf-field-group.scss',

0 commit comments

Comments
 (0)