@@ -26,8 +26,8 @@ function frak_add_admin_menu() {
2626
2727// Function to get the static config file path
2828function frak_get_config_file_path () {
29- $ upload_dir = wp_upload_dir ( );
30- $ frak_dir = $ upload_dir [ ' basedir ' ] . '/frak ' ;
29+ $ plugin_dir = plugin_dir_path ( __FILE__ );
30+ $ frak_dir = $ plugin_dir . 'assets/js ' ;
3131 if (!file_exists ($ frak_dir )) {
3232 wp_mkdir_p ($ frak_dir );
3333 }
@@ -36,8 +36,8 @@ function frak_get_config_file_path() {
3636
3737// Function to get the config file URL
3838function frak_get_config_file_url () {
39- $ upload_dir = wp_upload_dir ( );
40- return $ upload_dir [ ' baseurl ' ] . '/frak /config.js ' ;
39+ $ plugin_url = plugin_dir_url ( __FILE__ );
40+ return $ plugin_url . 'assets/js /config.js ' ;
4141}
4242
4343// Function to save the static config file
@@ -53,16 +53,6 @@ function frak_save_config_file($config_content) {
5353 return false ;
5454}
5555
56- // Add rewrite rules for the config file
57- function frak_add_rewrite_rules () {
58- add_rewrite_rule (
59- 'frak/config\.js$ ' ,
60- 'index.php?frak_config=1 ' ,
61- 'top '
62- );
63- }
64- add_action ('init ' , 'frak_add_rewrite_rules ' );
65-
6656// Handle the config file request
6757function frak_handle_config_request ($ wp ) {
6858 if (isset ($ wp ->query_vars ['frak_config ' ])) {
@@ -381,7 +371,7 @@ function frak_add_to_frontend() {
381371
382372 // Add the static config file with cache busting
383373 $ config_script = frak_get_config_file_url ();
384- if (! empty ( $ config_script )) {
374+ if (file_exists ( frak_get_config_file_path () )) {
385375 wp_enqueue_script (
386376 'frak-config ' ,
387377 $ config_script ,
@@ -408,14 +398,14 @@ function frak_add_to_frontend() {
408398}
409399add_action ('wp_enqueue_scripts ' , 'frak_add_to_frontend ' , 20 );
410400
411- // Add floating button to the body
401+ // Add floating button to footer
412402function frak_add_floating_button () {
413403 if (is_admin ()) {
414- return '' ;
404+ return ;
415405 }
416406
417407 if (!get_option ('frak_enable_floating_button ' , 0 )) {
418- return '' ;
408+ return ;
419409 }
420410
421411 $ show_reward = get_option ('frak_show_reward ' , 0 );
@@ -429,68 +419,18 @@ function frak_add_floating_button() {
429419 $ attributes [] = 'classname=" ' . esc_attr ($ classname ) . '" ' ;
430420 }
431421
432- return '<frak-button-wallet ' . implode (' ' , $ attributes ) . '></frak-button-wallet> ' ;
433- }
434-
435- // Add floating button to footer
436- function frak_output_floating_button () {
437- echo frak_add_floating_button ();
422+ echo '<frak-button-wallet ' . implode (' ' , $ attributes ) . '></frak-button-wallet> ' ;
438423}
439- add_action ('wp_footer ' , 'frak_output_floating_button ' );
440-
441- // Fallback function to inject scripts if wp_head/wp_footer are not available
442- function frak_fallback_injection () {
443- // Only inject if we haven't already and if we're not in admin
444- if (is_admin ()) {
445- return ;
446- }
447-
448- // Check if scripts are already in the output buffer to prevent duplicates
449- $ has_frak_script = wp_script_is ('frak-config ' , 'done ' ) && wp_script_is ('frak-components ' , 'done ' );
450- if ($ has_frak_script ) {
451- return ;
452- }
453-
454- $ to_add = '' ;
455-
456- // If we don't have the scripts, add them
457- if (!$ has_frak_script ) {
458- $ config_script = frak_get_config_file_url ();
459-
460- // Add config script if available
461- if (!empty ($ config_script )) {
462- $ last_modified = get_option ('frak_config_last_modified ' , 0 );
463- // Add a v=lastModified to the script tag to force cache busting
464- $ final_script_url = $ config_script . '?ver= ' . $ last_modified ;
465- $ to_add .= sprintf (
466- '<script src="%s" defer></script> ' ,
467- esc_url ($ final_script_url )
468- );
469- }
470-
471- // Add Frak components script
472- $ components_script_version = date ('zo ' );
473- $ final_components_script_url = 'https://cdn.jsdelivr.net/npm/@frak-labs/components@latest/cdn/components.js?ver= ' . $ components_script_version ;
474- $ to_add .= sprintf (
475- '<script src="%s" defer></script> ' ,
476- esc_url ($ final_components_script_url )
477- );
478- }
479-
480- // todo: Should find a way to add the button (but check with `ob_get_contents` are failing)
481-
482- // Add scripts to the output buffer
483- echo $ to_add ;
484- }
485- add_action ('shutdown ' , 'frak_fallback_injection ' );
424+ add_action ('wp_footer ' , 'frak_add_floating_button ' );
486425
487426// Add WooCommerce purchase tracking
488427function frak_add_purchase_tracking ($ order_id ) {
489- // Early exit if tracking is disabled
490- if (!get_option ('frak_enable_purchase_tracking ' , 0 )) {
428+ // Early exit if tracking is disabled or if in admin page
429+ if (!get_option ('frak_enable_purchase_tracking ' , 0 ) || is_admin () ) {
491430 return ;
492431 }
493432
433+ // If the order ID is not set, return
494434 if (!$ order_id ) return ;
495435
496436 $ order = wc_get_order ($ order_id );
@@ -500,9 +440,13 @@ function frak_add_purchase_tracking($order_id) {
500440 $ order_key = $ order ->get_order_key ();
501441 $ transaction_id = $ order ->get_transaction_id ();
502442
503- echo "<script>
504- const interactionToken = sessionStorage.getItem('frak-wallet-interaction-token');
505-
443+ // Instead of directly echoing the script, enqueue it properly
444+ wp_register_script ('frak-purchase-tracking ' , null );
445+ wp_enqueue_script ('frak-purchase-tracking ' );
446+ wp_add_inline_script ('frak-purchase-tracking ' , "
447+ try {
448+ const interactionToken = sessionStorage.getItem('frak-wallet-interaction-token');
449+ if (interactionToken) {
506450 const payload = {
507451 customerId: " . json_encode ($ customer_id ) . ",
508452 orderId: " . json_encode ($ order_id ) . ",
@@ -517,9 +461,15 @@ function frak_add_purchase_tracking($order_id) {
517461 'x-wallet-sdk-auth': interactionToken
518462 },
519463 body: JSON.stringify(payload)
464+ }).catch(error => {
465+ console.error('Error sending purchase tracking:', error);
520466 });
521- </script> " ;
467+ }
468+ } catch (error) {
469+ console.error('Error sending purchase tracking:', error);
470+ }
471+ " );
522472}
523473
524474// Hook into WooCommerce
525- add_action ('woocommerce_thankyou ' , 'frak_add_purchase_tracking ' , 10 , 1 );
475+ add_action ('woocommerce_thankyou ' , 'frak_add_purchase_tracking ' );
0 commit comments