1+ jQuery ( document ) . ready ( function ( $ ) {
2+ var editor ;
3+
4+ // Initialize code editor if available
5+ if ( typeof wp . codeEditor !== 'undefined' && $ ( '#frak_custom_config' ) . length ) {
6+ var editorSettings = wp . codeEditor . defaultSettings ? _ . clone ( wp . codeEditor . defaultSettings ) : { } ;
7+ editorSettings . codemirror = _ . extend ( { } , editorSettings . codemirror , {
8+ indentUnit : 4 ,
9+ tabSize : 4 ,
10+ mode : 'javascript' ,
11+ lineNumbers : true ,
12+ matchBrackets : true ,
13+ autoCloseBrackets : true ,
14+ extraKeys : { "Ctrl-Space" : "autocomplete" } ,
15+ theme : 'default'
16+ } ) ;
17+ editor = wp . codeEditor . initialize ( $ ( '#frak_custom_config' ) , editorSettings ) ;
18+ }
19+
20+ // Update config function
21+ function updateConfig ( ) {
22+ if ( ! editor ) return ;
23+
24+ var appName = $ ( '#frak_app_name' ) . val ( ) ;
25+ var logoUrl = $ ( '#frak_logo_url' ) . val ( ) ;
26+ var currentConfig = editor . codemirror . getValue ( ) ;
27+
28+ currentConfig = currentConfig . replace (
29+ / ( m e t a d a t a : \s * { \s * n a m e : \s * " ) [ ^ " ] * ( " ) / ,
30+ '$1' + appName + '$2'
31+ ) ;
32+
33+ currentConfig = currentConfig . replace (
34+ / ( l o g o U r l : \s * " ) [ ^ " ] * ( " ) / ,
35+ '$1' + logoUrl + '$2'
36+ ) ;
37+
38+ editor . codemirror . setValue ( currentConfig ) ;
39+ }
40+
41+ // Toggle floating button settings
42+ function toggleFloatingButtonSettings ( ) {
43+ var enabled = $ ( '#frak_enable_floating_button' ) . is ( ':checked' ) ;
44+ $ ( '#frak_show_reward, #frak_button_classname' ) . prop ( 'disabled' , ! enabled ) ;
45+ }
46+
47+ // Autofill functionality
48+ $ ( '#autofill_app_name' ) . on ( 'click' , function ( ) {
49+ if ( frak_ajax . site_info . name ) {
50+ $ ( '#frak_app_name' ) . val ( frak_ajax . site_info . name ) . trigger ( 'input' ) ;
51+ }
52+ } ) ;
53+
54+ $ ( '#autofill_logo_url' ) . on ( 'click' , function ( ) {
55+ if ( frak_ajax . site_info . logo_url ) {
56+ $ ( '#frak_logo_url' ) . val ( frak_ajax . site_info . logo_url ) . trigger ( 'input' ) ;
57+ }
58+ } ) ;
59+
60+ // Bind events
61+ $ ( '#frak_app_name, #frak_logo_url' ) . on ( 'input' , updateConfig ) ;
62+ $ ( '#frak_enable_floating_button' ) . on ( 'change' , toggleFloatingButtonSettings ) ;
63+ toggleFloatingButtonSettings ( ) ;
64+
65+ // Generate webhook secret
66+ $ ( '#generate-webhook-secret' ) . on ( 'click' , function ( e ) {
67+ e . preventDefault ( ) ;
68+
69+ if ( ! confirm ( 'Are you sure you want to regenerate the webhook secret? This will break the integration if you have already configured it on Frak.' ) ) {
70+ return ;
71+ }
72+
73+ $ . post ( frak_ajax . ajax_url , {
74+ action : 'frak_generate_webhook_secret' ,
75+ nonce : frak_ajax . nonce
76+ } , function ( response ) {
77+ if ( response . success ) {
78+ $ ( '#frak_webhook_secret' ) . val ( response . data . secret ) ;
79+ showNotice ( response . data . message , 'success' ) ;
80+ } else {
81+ showNotice ( 'Error generating webhook secret' , 'error' ) ;
82+ }
83+ } ) ;
84+ } ) ;
85+
86+ // Test webhook
87+ $ ( '#test-webhook' ) . on ( 'click' , function ( e ) {
88+ e . preventDefault ( ) ;
89+
90+ var $button = $ ( this ) ;
91+ $button . prop ( 'disabled' , true ) . text ( 'Testing...' ) ;
92+
93+ $ . post ( frak_ajax . ajax_url , {
94+ action : 'frak_test_webhook' ,
95+ nonce : frak_ajax . nonce
96+ } , function ( response ) {
97+ if ( response . success ) {
98+ showNotice ( response . data . message , 'success' ) ;
99+ } else {
100+ showNotice ( response . data . message , 'error' ) ;
101+ }
102+ } ) . always ( function ( ) {
103+ $button . prop ( 'disabled' , false ) . text ( 'Test Webhook' ) ;
104+ } ) ;
105+ } ) ;
106+
107+ // Clear webhook logs
108+ $ ( '#clear-webhook-logs' ) . on ( 'click' , function ( e ) {
109+ e . preventDefault ( ) ;
110+
111+ if ( ! confirm ( 'Are you sure you want to clear all webhook logs?' ) ) {
112+ return ;
113+ }
114+
115+ $ . post ( frak_ajax . ajax_url , {
116+ action : 'frak_clear_webhook_logs' ,
117+ nonce : frak_ajax . nonce
118+ } , function ( response ) {
119+ if ( response . success ) {
120+ showNotice ( response . data . message , 'success' ) ;
121+ setTimeout ( function ( ) {
122+ location . reload ( ) ;
123+ } , 1000 ) ;
124+ }
125+ } ) ;
126+ } ) ;
127+
128+ // Open webhook setup popup
129+ $ ( '#open-webhook-popup' ) . on ( 'click' , function ( e ) {
130+ e . preventDefault ( ) ;
131+
132+ var productId = $ ( this ) . data ( 'product-id' ) ;
133+ var webhookSecret = $ ( '#frak_webhook_secret' ) . val ( ) ;
134+
135+ if ( ! webhookSecret ) {
136+ alert ( 'Please generate a webhook secret first' ) ;
137+ return ;
138+ }
139+
140+ var createUrl = new URL ( 'https://business.frak.id' ) ;
141+ createUrl . pathname = '/embedded/purchase-tracker' ;
142+ createUrl . searchParams . append ( 'pid' , productId ) ;
143+ createUrl . searchParams . append ( 's' , webhookSecret ) ;
144+ createUrl . searchParams . append ( 'p' , 'custom' ) ;
145+
146+ var openedWindow = window . open (
147+ createUrl . href ,
148+ 'frak-business' ,
149+ 'menubar=no,status=no,scrollbars=no,fullscreen=no,width=500,height=800'
150+ ) ;
151+
152+ if ( openedWindow ) {
153+ openedWindow . focus ( ) ;
154+
155+ // Check when window is closed and refresh status
156+ var timer = setInterval ( function ( ) {
157+ if ( openedWindow . closed ) {
158+ clearInterval ( timer ) ;
159+ setTimeout ( function ( ) {
160+ checkWebhookStatus ( ) ;
161+ } , 1000 ) ;
162+ }
163+ } , 500 ) ;
164+ }
165+ } ) ;
166+
167+ // Check webhook status
168+ function checkWebhookStatus ( ) {
169+ $ . post ( frak_ajax . ajax_url , {
170+ action : 'frak_check_webhook_status' ,
171+ nonce : frak_ajax . nonce
172+ } , function ( response ) {
173+ if ( response . success ) {
174+ var $status = $ ( '.frak-webhook-status' ) ;
175+ if ( response . data . status ) {
176+ $status . removeClass ( 'status-inactive' ) . addClass ( 'status-active' ) . text ( 'Active' ) ;
177+ showNotice ( 'Webhook is now active!' , 'success' ) ;
178+ } else {
179+ $status . removeClass ( 'status-active' ) . addClass ( 'status-inactive' ) . text ( 'Inactive' ) ;
180+ }
181+ }
182+ } ) ;
183+ }
184+
185+ // Show admin notice
186+ function showNotice ( message , type ) {
187+ var $notice = $ ( '<div class="notice notice-' + type + ' is-dismissible"><p>' + message + '</p></div>' ) ;
188+ $ ( '.wrap h1' ) . after ( $notice ) ;
189+
190+ // Auto dismiss after 5 seconds
191+ setTimeout ( function ( ) {
192+ $notice . fadeOut ( function ( ) {
193+ $ ( this ) . remove ( ) ;
194+ } ) ;
195+ } , 5000 ) ;
196+ }
197+ } ) ;
0 commit comments