|
1 | 1 | /** |
2 | | - * Experiments TypeScript |
| 2 | + * Admin experiments. |
3 | 3 | * |
4 | 4 | * @package Secure Custom Fields |
5 | | - * @subpackage Admin |
6 | | - * @since 6.4.2 |
| 5 | + * @since 6.4.3 |
7 | 6 | */ |
8 | 7 |
|
9 | 8 | // Define interfaces for our data structures |
10 | 9 | interface ExperimentsData { |
11 | 10 | [key: string]: boolean; // Just store enabled status |
12 | 11 | } |
13 | 12 |
|
14 | | -interface ScfExperiments { |
15 | | - data: ExperimentsData; |
| 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; |
16 | 23 | } |
17 | 24 |
|
18 | 25 | // Declare global types |
19 | 26 | declare global { |
20 | 27 | interface Window { |
21 | | - scfExperiments?: ScfExperiments; |
22 | | - SCF?: { |
23 | | - experiments?: { |
24 | | - data: ExperimentsData; |
25 | | - init: () => void; |
26 | | - isEnabled: (name: string) => boolean; |
27 | | - }; |
28 | | - }; |
| 28 | + acf: ACF; // Ensure acf is available on window |
| 29 | + acfExperiments?: ExperimentsData; // Variable created by wp_localize_script |
29 | 30 | } |
| 31 | + var acf: ACF; |
| 32 | + var acfExperiments: ExperimentsData | undefined; // Variable created by wp_localize_script |
30 | 33 | } |
31 | 34 |
|
32 | 35 | // Create a module to avoid global scope augmentation issues |
33 | 36 | export {}; |
34 | 37 |
|
35 | | -// Initialize the SCF namespace if it doesn't exist |
36 | | -if (typeof window.SCF === 'undefined') { |
37 | | - window.SCF = {}; |
38 | | -} |
39 | | - |
40 | | -// Initialize experiments module |
41 | | -window.SCF.experiments = { |
42 | | - data: {}, |
43 | | - init: function(): void { |
44 | | - // Get experiments data from localized script |
45 | | - this.data = window.scfExperiments ? window.scfExperiments.data : {}; |
46 | | - |
47 | | - // Log experiments data to console for debugging |
48 | | - console.log('SCF Experiments initialized:', this.data); |
49 | | - }, |
50 | | - isEnabled: function(name: string): boolean { |
51 | | - return !!this.data[name]; |
| 38 | +(function() { |
| 39 | + // End the function if acf doesn't exist |
| 40 | + if (typeof acf !== 'object' || acf === null) { |
| 41 | + return; |
52 | 42 | } |
53 | | -}; |
54 | 43 |
|
55 | | -// Initialize when document is ready |
56 | | -document.addEventListener('DOMContentLoaded', () => { |
57 | | - if (window.SCF?.experiments) { |
58 | | - window.SCF.experiments.init(); |
59 | | - } |
60 | | -}); |
| 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 | + }); |
61 | 66 |
|
62 | | -// Log initial state |
63 | | -console.log("SCF Experiments module loaded"); |
| 67 | +})(); |
0 commit comments