Skip to content
This repository was archived by the owner on Apr 21, 2026. It is now read-only.

Commit 1a7b368

Browse files
committed
feat: Save config to cached file
1 parent c8c3fa9 commit 1a7b368

1 file changed

Lines changed: 86 additions & 12 deletions

File tree

frak-integration.php

Lines changed: 86 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*
33
Plugin Name: Frak
44
Description: Adds Frak configuration to your WordPress site
5-
Version: 0.3
5+
Version: 0.4
66
Author: Frak-Labs
77
*/
88

@@ -24,6 +24,86 @@ function frak_add_admin_menu() {
2424
}
2525
add_action('admin_menu', 'frak_add_admin_menu');
2626

27+
// Function to get the static config file path
28+
function frak_get_config_file_path() {
29+
$upload_dir = wp_upload_dir();
30+
$frak_dir = $upload_dir['basedir'] . '/frak';
31+
if (!file_exists($frak_dir)) {
32+
wp_mkdir_p($frak_dir);
33+
}
34+
return $frak_dir . '/config.js';
35+
}
36+
37+
// Function to get the config file URL
38+
function frak_get_config_file_url() {
39+
$upload_dir = wp_upload_dir();
40+
return $upload_dir['baseurl'] . '/frak/config.js';
41+
}
42+
43+
// Function to save the static config file
44+
function frak_save_config_file($config_content) {
45+
$file_path = frak_get_config_file_path();
46+
$result = file_put_contents($file_path, $config_content);
47+
48+
if ($result !== false) {
49+
// Update the last modification timestamp
50+
update_option('frak_config_last_modified', time());
51+
return true;
52+
}
53+
return false;
54+
}
55+
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+
67+
// Add rewrite rules for the config file
68+
function frak_add_rewrite_rules() {
69+
add_rewrite_rule(
70+
'frak/config\.js$',
71+
'index.php?frak_config=1',
72+
'top'
73+
);
74+
}
75+
add_action('init', 'frak_add_rewrite_rules');
76+
77+
// Handle the config file request
78+
function frak_handle_config_request($wp) {
79+
if (isset($wp->query_vars['frak_config'])) {
80+
$file_path = frak_get_config_file_path();
81+
82+
if (file_exists($file_path)) {
83+
$last_modified = get_option('frak_config_last_modified', 0);
84+
85+
// Set proper headers for caching
86+
header('Content-Type: application/javascript');
87+
header('Cache-Control: public, max-age=31536000'); // 1 year
88+
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $last_modified) . ' GMT');
89+
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 31536000) . ' GMT');
90+
91+
// Check if the browser has a cached version
92+
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
93+
$if_modified_since = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
94+
if ($if_modified_since >= $last_modified) {
95+
header('HTTP/1.1 304 Not Modified');
96+
exit;
97+
}
98+
}
99+
100+
readfile($file_path);
101+
exit;
102+
}
103+
}
104+
}
105+
add_action('parse_request', 'frak_handle_config_request');
106+
27107
// Register settings
28108
function frak_register_settings() {
29109
register_setting('frak_settings', 'frak_app_name');
@@ -90,6 +170,9 @@ function frak_settings_page() {
90170
update_option('frak_enable_floating_button', $enable_button);
91171
update_option('frak_show_reward', $show_reward);
92172
update_option('frak_button_classname', $button_classname);
173+
174+
// Save the config to a static file
175+
frak_save_config_file($custom_config);
93176
}
94177

95178
$app_name = get_option('frak_app_name', 'Your App Name');
@@ -307,17 +390,8 @@ function frak_add_to_frontend() {
307390
return;
308391
}
309392

310-
$custom_config = get_option('frak_custom_config');
311-
if (!empty($custom_config)) {
312-
// Remove whitespace from the config
313-
$custom_config = preg_replace('/\s+/', ' ', $custom_config);
314-
$custom_config = preg_replace('/\s*([{}:,=+\-*\/()])\s*/', '$1', $custom_config);
315-
316-
// Add it to the header
317-
echo "<script>\n";
318-
echo stripslashes($custom_config);
319-
echo "\n</script>\n";
320-
}
393+
// Add the static config file with cache busting
394+
echo frak_get_config_script_tag();
321395
?>
322396
<script src="https://cdn.jsdelivr.net/npm/@frak-labs/components@latest/cdn/components.js" defer="defer"></script>
323397
<?php

0 commit comments

Comments
 (0)