22/*
33Plugin Name: Frak
44Description: Adds Frak configuration to your WordPress site
5- Version: 0.4
5+ Version: 0.5
66Author: Frak-Labs
77*/
88
@@ -53,17 +53,6 @@ function frak_save_config_file($config_content) {
5353 return false ;
5454}
5555
56- // Function to get the config file with cache busting
57- function frak_get_config_script_tag () {
58- $ last_modified = get_option ('frak_config_last_modified ' , 0 );
59- $ config_url = frak_get_config_file_url ();
60- return sprintf (
61- '<script src="%s?v=%d" type="text/javascript"></script> ' ,
62- esc_url ($ config_url ),
63- $ last_modified
64- );
65- }
66-
6756// Add rewrite rules for the config file
6857function frak_add_rewrite_rules () {
6958 add_rewrite_rule (
@@ -391,21 +380,42 @@ function frak_add_to_frontend() {
391380 }
392381
393382 // Add the static config file with cache busting
394- echo frak_get_config_script_tag ();
395- ?>
396- <script src="https://cdn.jsdelivr.net/npm/@frak-labs/components@latest/cdn/components.js" defer="defer"></script>
397- <?php
383+ $ config_script = frak_get_config_file_url ();
384+ if (!empty ($ config_script )) {
385+ wp_enqueue_script (
386+ 'frak-config ' ,
387+ $ config_script ,
388+ array (),
389+ get_option ('frak_config_last_modified ' , 0 ),
390+ array (
391+ 'strategy ' => 'defer '
392+ )
393+ );
394+ }
395+
396+ // The version of the components script correspond to the current day of the year, to ensure it's not cached more than 24hr
397+ $ components_script_version = date ('zo ' );
398+ // Add the Frak components script
399+ wp_enqueue_script (
400+ 'frak-components ' ,
401+ 'https://cdn.jsdelivr.net/npm/@frak-labs/components@latest/cdn/components.js ' ,
402+ array (),
403+ $ components_script_version ,
404+ array (
405+ 'strategy ' => 'defer '
406+ )
407+ );
398408}
399- add_action ('wp_head ' , 'frak_add_to_frontend ' );
409+ add_action ('wp_enqueue_scripts ' , 'frak_add_to_frontend ' , 20 );
400410
401411// Add floating button to the body
402412function frak_add_floating_button () {
403413 if (is_admin ()) {
404- return ;
414+ return '' ;
405415 }
406416
407417 if (!get_option ('frak_enable_floating_button ' , 0 )) {
408- return ;
418+ return '' ;
409419 }
410420
411421 $ show_reward = get_option ('frak_show_reward ' , 0 );
@@ -419,9 +429,60 @@ function frak_add_floating_button() {
419429 $ attributes [] = 'classname=" ' . esc_attr ($ classname ) . '" ' ;
420430 }
421431
422- echo '<frak-button-wallet ' . implode (' ' , $ attributes ) . '></frak-button-wallet> ' ;
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 ();
438+ }
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 ;
423484}
424- add_action ('wp_footer ' , 'frak_add_floating_button ' );
485+ add_action ('shutdown ' , 'frak_fallback_injection ' );
425486
426487// Add WooCommerce purchase tracking
427488function frak_add_purchase_tracking ($ order_id ) {
0 commit comments