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

Commit 44709df

Browse files
authored
Merge pull request #4 from frak-id/feat/reorganize-settings-page
Reorganize settings page and add modal customization options
2 parents f3c43d9 + 5002f88 commit 44709df

7 files changed

Lines changed: 1068 additions & 277 deletions

File tree

admin/class-frak-admin.php

Lines changed: 130 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ public function register_settings() {
4545
register_setting('frak_settings', 'frak_enable_floating_button');
4646
register_setting('frak_settings', 'frak_show_reward');
4747
register_setting('frak_settings', 'frak_button_classname');
48+
register_setting('frak_settings', 'frak_floating_button_position');
49+
register_setting('frak_settings', 'frak_modal_language');
50+
register_setting('frak_settings', 'frak_modal_i18n', array(
51+
'sanitize_callback' => function($input) {
52+
if (is_array($input)) {
53+
return json_encode(array_filter($input, function($value) {
54+
return $value !== '';
55+
}));
56+
}
57+
return $input;
58+
}
59+
));
4860
register_setting('frak_settings', 'frak_webhook_secret');
4961
}
5062

@@ -55,6 +67,7 @@ public function enqueue_scripts($hook) {
5567

5668
wp_enqueue_code_editor(array('type' => 'text/javascript'));
5769
wp_enqueue_script('frak-admin', plugin_dir_url(dirname(__FILE__)) . 'admin/js/admin.js', array('jquery'), '1.0', true);
70+
wp_enqueue_style('frak-admin', plugin_dir_url(dirname(__FILE__)) . 'admin/css/admin.css', array(), '1.0');
5871

5972
// Get logo URL for autofill
6073
$logo_url = '';
@@ -155,12 +168,31 @@ public function settings_page() {
155168

156169
private function save_settings() {
157170
$app_name = sanitize_text_field($_POST['frak_app_name']);
171+
172+
// Handle file upload if present
158173
$logo_url = esc_url_raw($_POST['frak_logo_url']);
174+
if (!empty($_FILES['frak_logo_file']) && $_FILES['frak_logo_file']['error'] == UPLOAD_ERR_OK) {
175+
$uploaded_logo = $this->handle_logo_upload($_FILES['frak_logo_file']);
176+
if ($uploaded_logo) {
177+
$logo_url = $uploaded_logo;
178+
}
179+
}
180+
159181
$custom_config = stripslashes($_POST['frak_custom_config']);
160182
$enable_tracking = isset($_POST['frak_enable_purchase_tracking']) ? 1 : 0;
161183
$enable_button = isset($_POST['frak_enable_floating_button']) ? 1 : 0;
162184
$show_reward = isset($_POST['frak_show_reward']) ? 1 : 0;
163185
$button_classname = isset($_POST['frak_button_classname']) ? sanitize_text_field($_POST['frak_button_classname']) : '';
186+
$floating_button_position = isset($_POST['frak_floating_button_position']) ? sanitize_text_field($_POST['frak_floating_button_position']) : 'right';
187+
$modal_language = isset($_POST['frak_modal_language']) ? sanitize_text_field($_POST['frak_modal_language']) : 'en';
188+
189+
// Handle modal i18n
190+
$modal_i18n = isset($_POST['frak_modal_i18n']) ? $_POST['frak_modal_i18n'] : array();
191+
if (is_array($modal_i18n)) {
192+
$modal_i18n = array_filter($modal_i18n, function($value) {
193+
return $value !== '';
194+
});
195+
}
164196

165197
update_option('frak_app_name', $app_name);
166198
update_option('frak_logo_url', $logo_url);
@@ -169,6 +201,14 @@ private function save_settings() {
169201
update_option('frak_enable_floating_button', $enable_button);
170202
update_option('frak_show_reward', $show_reward);
171203
update_option('frak_button_classname', $button_classname);
204+
update_option('frak_floating_button_position', $floating_button_position);
205+
update_option('frak_modal_language', $modal_language);
206+
update_option('frak_modal_i18n', json_encode($modal_i18n));
207+
208+
// Update config last modified timestamp for cache busting
209+
if (class_exists('Frak_Config_Endpoint')) {
210+
Frak_Config_Endpoint::update_last_modified();
211+
}
172212

173213
}
174214

@@ -180,10 +220,20 @@ private function render_settings_page() {
180220
$app_name = get_option('frak_app_name', $default_app_name);
181221
$logo_url = get_option('frak_logo_url', $default_logo_url);
182222
$custom_config = get_option('frak_custom_config', '');
183-
$enable_tracking = get_option('frak_enable_purchase_tracking', 0);
223+
// Auto-enable WooCommerce tracking if WooCommerce is active and setting hasn't been configured yet
224+
$enable_tracking_option = get_option('frak_enable_purchase_tracking', null);
225+
if ($enable_tracking_option === null && class_exists('WooCommerce')) {
226+
$enable_tracking = 1;
227+
update_option('frak_enable_purchase_tracking', 1);
228+
} else {
229+
$enable_tracking = get_option('frak_enable_purchase_tracking', 0);
230+
}
184231
$enable_button = get_option('frak_enable_floating_button', 0);
185232
$show_reward = get_option('frak_show_reward', 0);
186233
$button_classname = get_option('frak_button_classname', '');
234+
$floating_button_position = get_option('frak_floating_button_position', 'right');
235+
$modal_language = get_option('frak_modal_language', 'default');
236+
$modal_i18n = json_decode(get_option('frak_modal_i18n', '{}'), true);
187237

188238
if (empty($custom_config)) {
189239
$custom_config = $this->get_default_config($app_name, $logo_url);
@@ -214,48 +264,94 @@ private function get_site_icon_url() {
214264
}
215265

216266
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+
217274
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+
218295
window.FrakSetup = {
219-
config: {
220-
metadata: {
221-
name: "{$app_name}",
222-
lang: "en",
223-
currency: "eur",
224-
logoUrl: "{$logo_url}",
225-
homepageLink: window.location.origin,
226-
},
227-
customizations: {},
228-
domain: window.location.host,
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
229305
},
230-
modalConfig: {
231-
login: {
232-
allowSso: true,
233-
ssoMetadata: {},
234-
},
235-
metadata: {
236-
isDismissible: true,
237-
},
306+
modalConfig: {
307+
login: {
308+
allowSso: true,
309+
ssoMetadata: {
310+
logoUrl,
311+
homepageLink: window.location.host
312+
}
313+
}
238314
},
239-
modalShareConfig: {
240-
link: window.location.href,
241-
},
242-
modalWalletConfig: {
243-
metadata: {
244-
position: "left",
245-
},
246-
loggedIn: {
247-
action: {
248-
key: "sharing",
249-
options: {
250-
link: window.location.href,
251-
},
252-
},
253-
},
315+
modalShareConfig: {
316+
link: window.location.href
317+
},
318+
modalWalletConfig: {
319+
metadata: {
320+
position: '{$floating_button_position}'
321+
}
254322
},
255323
};
256324
JS;
257325
}
258326

327+
private function handle_logo_upload($file) {
328+
// Check file type
329+
$allowed_types = array('image/jpeg', 'image/png', 'image/gif', 'image/svg+xml');
330+
if (!in_array($file['type'], $allowed_types)) {
331+
return false;
332+
}
333+
334+
// Check file size (2MB max)
335+
if ($file['size'] > 2 * 1024 * 1024) {
336+
return false;
337+
}
338+
339+
// Use WordPress media upload handler
340+
if (!function_exists('wp_handle_upload')) {
341+
require_once(ABSPATH . 'wp-admin/includes/file.php');
342+
}
343+
344+
$upload_overrides = array('test_form' => false);
345+
$movefile = wp_handle_upload($file, $upload_overrides);
346+
347+
if ($movefile && !isset($movefile['error'])) {
348+
// Upload successful
349+
return $movefile['url'];
350+
}
351+
352+
return false;
353+
}
354+
259355
// AJAX Handlers
260356
public function ajax_generate_webhook_secret() {
261357
check_ajax_referer('frak_ajax_nonce', 'nonce');

0 commit comments

Comments
 (0)