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

Commit 3d30d9e

Browse files
authored
Merge pull request #5 from frak-id/remove-live-preview-generate-js-backend
Remove live preview and generate JS on backend only
2 parents 44709df + 472059d commit 3d30d9e

10 files changed

Lines changed: 307 additions & 515 deletions

admin/class-frak-admin.php

Lines changed: 57 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ public function add_admin_menu() {
3636
public function register_settings() {
3737
register_setting('frak_settings', 'frak_app_name');
3838
register_setting('frak_settings', 'frak_logo_url');
39-
register_setting('frak_settings', 'frak_custom_config', array(
40-
'sanitize_callback' => function($input) {
41-
return stripslashes($input);
42-
}
43-
));
4439
register_setting('frak_settings', 'frak_enable_purchase_tracking');
4540
register_setting('frak_settings', 'frak_enable_floating_button');
4641
register_setting('frak_settings', 'frak_show_reward');
@@ -178,7 +173,6 @@ private function save_settings() {
178173
}
179174
}
180175

181-
$custom_config = stripslashes($_POST['frak_custom_config']);
182176
$enable_tracking = isset($_POST['frak_enable_purchase_tracking']) ? 1 : 0;
183177
$enable_button = isset($_POST['frak_enable_floating_button']) ? 1 : 0;
184178
$show_reward = isset($_POST['frak_show_reward']) ? 1 : 0;
@@ -189,14 +183,29 @@ private function save_settings() {
189183
// Handle modal i18n
190184
$modal_i18n = isset($_POST['frak_modal_i18n']) ? $_POST['frak_modal_i18n'] : array();
191185
if (is_array($modal_i18n)) {
186+
// Sanitize each value
187+
foreach ($modal_i18n as $key => $value) {
188+
// For text areas, preserve line breaks but sanitize content
189+
if (in_array($key, ['sharing.text', 'sdk.wallet.login.text_sharing', 'sdk.wallet.login.text_referred'])) {
190+
$modal_i18n[$key] = sanitize_textarea_field($value);
191+
} else {
192+
$modal_i18n[$key] = sanitize_text_field($value);
193+
}
194+
}
195+
196+
// Remove empty values
192197
$modal_i18n = array_filter($modal_i18n, function($value) {
193198
return $value !== '';
194199
});
200+
201+
// Handle special case: if text_referred is set, also set it as text
202+
if (isset($modal_i18n['sdk.wallet.login.text_referred'])) {
203+
$modal_i18n['sdk.wallet.login.text'] = $modal_i18n['sdk.wallet.login.text_referred'];
204+
}
195205
}
196206

197207
update_option('frak_app_name', $app_name);
198208
update_option('frak_logo_url', $logo_url);
199-
update_option('frak_custom_config', $custom_config);
200209
update_option('frak_enable_purchase_tracking', $enable_tracking);
201210
update_option('frak_enable_floating_button', $enable_button);
202211
update_option('frak_show_reward', $show_reward);
@@ -205,21 +214,57 @@ private function save_settings() {
205214
update_option('frak_modal_language', $modal_language);
206215
update_option('frak_modal_i18n', json_encode($modal_i18n));
207216

208-
// Update config last modified timestamp for cache busting
209-
if (class_exists('Frak_Config_Endpoint')) {
210-
Frak_Config_Endpoint::update_last_modified();
211-
}
217+
// Clear any caches to ensure the new configuration is loaded
218+
$this->clear_caches();
212219

213220
}
214221

222+
/**
223+
* Clear caches from popular caching plugins
224+
*/
225+
private function clear_caches() {
226+
// WP Rocket
227+
if (function_exists('rocket_clean_domain')) {
228+
rocket_clean_domain();
229+
}
230+
231+
// W3 Total Cache
232+
if (function_exists('w3tc_flush_all')) {
233+
w3tc_flush_all();
234+
}
235+
236+
// WP Super Cache
237+
if (function_exists('wp_cache_clear_cache')) {
238+
wp_cache_clear_cache();
239+
}
240+
241+
// WP Fastest Cache
242+
if (class_exists('WpFastestCache') && method_exists('WpFastestCache', 'deleteCache')) {
243+
$wpfc = new WpFastestCache();
244+
$wpfc->deleteCache(true);
245+
}
246+
247+
// LiteSpeed Cache
248+
if (class_exists('LiteSpeed\Purge')) {
249+
LiteSpeed\Purge::purge_all();
250+
}
251+
252+
// Autoptimize
253+
if (class_exists('autoptimizeCache') && method_exists('autoptimizeCache', 'clearall')) {
254+
autoptimizeCache::clearall();
255+
}
256+
257+
// Clear WordPress object cache
258+
wp_cache_flush();
259+
}
260+
215261
private function render_settings_page() {
216262
// Get default values from WordPress site info
217263
$default_app_name = get_bloginfo('name');
218264
$default_logo_url = $this->get_site_icon_url();
219265

220266
$app_name = get_option('frak_app_name', $default_app_name);
221267
$logo_url = get_option('frak_logo_url', $default_logo_url);
222-
$custom_config = get_option('frak_custom_config', '');
223268
// Auto-enable WooCommerce tracking if WooCommerce is active and setting hasn't been configured yet
224269
$enable_tracking_option = get_option('frak_enable_purchase_tracking', null);
225270
if ($enable_tracking_option === null && class_exists('WooCommerce')) {
@@ -235,10 +280,6 @@ private function render_settings_page() {
235280
$modal_language = get_option('frak_modal_language', 'default');
236281
$modal_i18n = json_decode(get_option('frak_modal_i18n', '{}'), true);
237282

238-
if (empty($custom_config)) {
239-
$custom_config = $this->get_default_config($app_name, $logo_url);
240-
}
241-
242283
include FRAK_PLUGIN_DIR . 'admin/views/settings-page.php';
243284
}
244285

@@ -263,66 +304,6 @@ private function get_site_icon_url() {
263304
return '';
264305
}
265306

266-
private function get_default_config($app_name, $logo_url) {
267-
$modal_language = get_option('frak_modal_language', 'default');
268-
$floating_button_position = get_option('frak_floating_button_position', 'right');
269-
$modal_i18n = get_option('frak_modal_i18n', '{}');
270-
271-
// Handle language setting
272-
$lang_code = $modal_language === 'default' ? 'undefined' : "'{$modal_language}'";
273-
274-
return <<<JS
275-
let logoUrl = '{$logo_url}';
276-
const lang = {$lang_code};
277-
278-
let i18n = {};
279-
try {
280-
i18n = JSON.parse('{$modal_i18n}'.replace(
281-
/&amp;|&lt;|&gt;|&#39;|&quot;/g,
282-
tag =>
283-
({
284-
'&amp;': '&',
285-
'&lt;': '<',
286-
'&gt;': '>',
287-
'&#39;': "'",
288-
'&quot;': '"'
289-
}[tag] || tag)
290-
)) || {};
291-
} catch (error) {
292-
console.error('Error parsing i18n customizations:', error);
293-
}
294-
295-
window.FrakSetup = {
296-
config: {
297-
walletUrl: 'https://wallet.frak.id',
298-
metadata: {
299-
name: '{$app_name}',
300-
lang,
301-
logoUrl
302-
},
303-
customizations: { i18n },
304-
domain: window.location.host
305-
},
306-
modalConfig: {
307-
login: {
308-
allowSso: true,
309-
ssoMetadata: {
310-
logoUrl,
311-
homepageLink: window.location.host
312-
}
313-
}
314-
},
315-
modalShareConfig: {
316-
link: window.location.href
317-
},
318-
modalWalletConfig: {
319-
metadata: {
320-
position: '{$floating_button_position}'
321-
}
322-
},
323-
};
324-
JS;
325-
}
326307

327308
private function handle_logo_upload($file) {
328309
// Check file type

admin/css/admin.css

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,6 @@
131131
color: #dc3232;
132132
}
133133

134-
/* Advanced configuration textarea */
135-
#frak_custom_config {
136-
background: #f0f0f1;
137-
border: 1px solid #8c8f94;
138-
}
139134

140135
/* Button spacing */
141136
.button + .button {
@@ -154,7 +149,7 @@
154149
}
155150

156151
.frak-i18n-table td:first-child {
157-
width: 150px;
152+
width: 250px;
158153
font-weight: 600;
159154
color: #23282d;
160155
}
@@ -163,6 +158,40 @@
163158
width: 100%;
164159
}
165160

161+
.frak-i18n-table textarea {
162+
width: 100%;
163+
resize: vertical;
164+
}
165+
166+
/* i18n group styling */
167+
.frak-i18n-group {
168+
margin-bottom: 20px;
169+
padding: 15px;
170+
background: #f5f5f5;
171+
border: 1px solid #e0e0e0;
172+
border-radius: 3px;
173+
}
174+
175+
.frak-i18n-group h4 {
176+
margin: 0 0 10px 0;
177+
color: #23282d;
178+
font-size: 14px;
179+
font-weight: 600;
180+
}
181+
182+
.frak-i18n-group .description {
183+
font-size: 13px;
184+
color: #666;
185+
line-height: 1.5;
186+
}
187+
188+
.frak-i18n-group .description code {
189+
background: #e0e0e0;
190+
padding: 2px 4px;
191+
border-radius: 3px;
192+
font-size: 12px;
193+
}
194+
166195
/* Logo preview */
167196
.frak-logo-preview {
168197
margin-top: 10px;

admin/js/admin.js

Lines changed: 0 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,4 @@
11
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 position = $('#frak_floating_button_position').val();
27-
var modalLang = $('#frak_modal_language').val();
28-
var currentConfig = editor.codemirror.getValue();
29-
30-
// Collect i18n values
31-
var i18nValues = {};
32-
$('.frak-i18n-table input').each(function() {
33-
var $input = $(this);
34-
var key = $input.attr('name').replace('frak_modal_i18n[', '').replace(']', '');
35-
var value = $input.val();
36-
if (value) {
37-
i18nValues[key] = value;
38-
}
39-
});
40-
41-
// Update app name
42-
currentConfig = currentConfig.replace(
43-
/(metadata:\s*{\s*name:\s*")[^"]*(")/,
44-
'$1' + appName + '$2'
45-
);
46-
47-
// Update logo URL - both in let declaration and in objects
48-
currentConfig = currentConfig.replace(
49-
/(let logoUrl = ')[^']*(')/,
50-
'$1' + logoUrl + '$2'
51-
);
52-
53-
// Update language - handle both string and variable format
54-
if (modalLang === 'default') {
55-
// Replace with undefined (variable)
56-
currentConfig = currentConfig.replace(
57-
/(const lang = )[^;]+/,
58-
'$1undefined'
59-
);
60-
} else {
61-
// Replace with quoted string
62-
currentConfig = currentConfig.replace(
63-
/(const lang = )[^;]+/,
64-
"$1'" + modalLang + "'"
65-
);
66-
}
67-
68-
// Update i18n object
69-
var i18nJson = JSON.stringify(i18nValues, null, 0);
70-
// Find the i18n parsing section and update it
71-
var i18nMatch = currentConfig.match(/let i18n = {};\s*try {[\s\S]*?} catch/);
72-
if (i18nMatch) {
73-
// Replace the entire i18n section
74-
var newI18nSection = "let i18n = {};\ntry {\n i18n = JSON.parse('" +
75-
i18nJson.replace(/'/g, "\\'") +
76-
"'.replace(\n /&amp;|&lt;|&gt;|&#39;|&quot;/g,\n tag =>\n ({\n '&amp;': '&',\n '&lt;': '<',\n '&gt;': '>',\n '&#39;': \"'\",\n '&quot;': '"'\n }[tag] || tag)\n )) || {};\n} catch";
77-
currentConfig = currentConfig.replace(/let i18n = {};\s*try {[\s\S]*?} catch/, newI18nSection);
78-
}
79-
80-
// Update floating button position
81-
currentConfig = currentConfig.replace(
82-
/(modalWalletConfig:\s*{[\s\S]*?metadata:\s*{[\s\S]*?position:\s*")[^"]*(")/,
83-
'$1' + position + '$2'
84-
);
85-
86-
editor.codemirror.setValue(currentConfig);
87-
}
88-
892
// Toggle floating button settings
903
function toggleFloatingButtonSettings() {
914
var enabled = $('#frak_enable_floating_button').is(':checked');
@@ -140,14 +53,10 @@ jQuery(document).ready(function($) {
14053
});
14154

14255
// Bind events
143-
$('#frak_app_name, #frak_logo_url, #frak_floating_button_position, #frak_modal_language').on('input change', updateConfig);
14456
$('#frak_enable_floating_button').on('change', toggleFloatingButtonSettings);
14557
$('#frak_logo_file').on('change', handleLogoFileSelect);
14658
$('#frak_logo_url').on('input', handleLogoUrlChange);
14759

148-
// Bind i18n input events
149-
$('.frak-i18n-table input').on('input', updateConfig);
150-
15160
// Initialize toggles
15261
toggleFloatingButtonSettings();
15362

0 commit comments

Comments
 (0)