-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme-customizer.php
More file actions
638 lines (534 loc) · 19.7 KB
/
theme-customizer.php
File metadata and controls
638 lines (534 loc) · 19.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
<?php
# Prevent direct access
if (!defined('IN_GS')) { die('You cannot load this file directly!'); }
# Plugin info
$thisfile = basename(__FILE__, ".php");
# Define constants early
define('TC_PLUGIN_ID', $thisfile);
define('TC_PLUGIN_PATH', GSPLUGINPATH . $thisfile . '/');
define('TC_DATA_PATH', GSDATAOTHERPATH . $thisfile . '/');
define('TC_SETTINGS_FILE', TC_DATA_PATH . 'settings.xml');
# Load i18n
i18n_merge(TC_PLUGIN_ID) || i18n_merge(TC_PLUGIN_ID, 'en_US');
# Register plugin
register_plugin(
$thisfile,
i18n_r(TC_PLUGIN_ID . '/PLUGIN_NAME'),
'1.0.0',
'Multicolor',
'https://ko-fi.com/multicolorplugins',
i18n_r(TC_PLUGIN_ID . '/PLUGIN_DESC'),
'theme',
'tc_main_router'
);
# Add admin menus with translations
add_action('theme-sidebar', 'createSideMenu', array(
$thisfile,
i18n_r(TC_PLUGIN_ID . '/MENU_CUSTOMIZER')
));
add_action('theme-sidebar', 'createSideMenu', array(
$thisfile . '&help',
i18n_r(TC_PLUGIN_ID . '/MENU_HELP')
));
# IMPORTANT: Intercept AJAX before generating HTML
add_action('admin-pre-header', 'tc_intercept_ajax');
# Create data directory
if (!file_exists(TC_DATA_PATH)) {
mkdir(TC_DATA_PATH, 0755, true);
}
# Include main class
require_once(TC_PLUGIN_PATH . 'inc/class.customizer.php');
# Global variables for API
$tc_customizer_instance = null;
/**
* Auto-override tc_get_option in preview mode
*/
add_action('index-pretemplate', 'tc_auto_preview_override', 1);
function tc_auto_preview_override() {
if (isset($_GET['tc_preview']) && $_GET['tc_preview'] == '1') {
// Store preview values in global variable
global $tc_preview_overrides;
$tc_preview_overrides = array();
foreach ($_GET as $key => $value) {
if (strpos($key, 'tc_') === 0 && $key !== 'tc_preview') {
$setting_id = substr($key, 3); // Remove 'tc_' prefix
$tc_preview_overrides[$setting_id] = $value;
}
}
}
}
/**
* Restore original settings after template render
*/
add_action('index-posttemplate', 'tc_auto_preview_restore', 999);
function tc_auto_preview_restore() {
global $tc_preview_original_settings;
if (isset($tc_preview_original_settings)) {
tc_get_customizer()->settings = $tc_preview_original_settings;
}
}
/**
* Intercept AJAX requests BEFORE any output
*/
function tc_intercept_ajax() {
// Check if this is our AJAX request
if (!empty($_POST['tc_ajax_action']) && isset($_GET['id']) && $_GET['id'] === 'theme-customizer') {
// Stop ALL output buffering
while (ob_get_level()) {
ob_end_clean();
}
// Call AJAX handler
tc_ajax_handler();
exit; // Stop everything
}
}
/**
* Intercept image upload requests
*/
add_action('admin-pre-header', 'tc_intercept_upload');
function tc_intercept_upload() {
// Check if this is our upload request
if (!empty($_FILES['tc_image']) && isset($_GET['id']) && $_GET['id'] === 'theme-customizer' && isset($_GET['upload'])) {
// Stop ALL output buffering
while (ob_get_level()) {
ob_end_clean();
}
tc_handle_image_upload();
exit;
}
}
/**
* Handle image upload
*/
function tc_handle_image_upload() {
header('Content-Type: application/json');
// Check authorization
if (!cookie_check()) {
echo json_encode(array(
'success' => false,
'message' => i18n_r(TC_PLUGIN_ID . '/MSG_NOT_AUTHORIZED')
));
exit;
}
if (!isset($_FILES['tc_image'])) {
echo json_encode(array(
'success' => false,
'message' => i18n_r(TC_PLUGIN_ID . '/ERROR_NO_FILE')
));
exit;
}
$file = $_FILES['tc_image'];
// Validate image
$allowed = array('jpg', 'jpeg', 'png', 'gif', 'webp', 'svg');
$filename = $file['name'];
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
if (!in_array($ext, $allowed)) {
$allowed_types = implode(', ', array_map('strtoupper', $allowed));
echo json_encode(array(
'success' => false,
'message' => i18n_r(TC_PLUGIN_ID . '/MSG_INVALID_FILE') . '. ' .
i18n_r(TC_PLUGIN_ID . '/UPLOAD_ALLOWED') . ': ' . $allowed_types
));
exit;
}
// Check file size (max 5MB)
if ($file['size'] > 5 * 1024 * 1024) {
echo json_encode(array(
'success' => false,
'message' => i18n_r(TC_PLUGIN_ID . '/MSG_FILE_TOO_LARGE')
));
exit;
}
// Check for upload errors
if ($file['error'] !== UPLOAD_ERR_OK) {
echo json_encode(array(
'success' => false,
'message' => i18n_r(TC_PLUGIN_ID . '/ERROR_UPLOAD') . ': ' . $file['error']
));
exit;
}
// Generate unique filename
$new_filename = time() . '_' . preg_replace('/[^a-zA-Z0-9._-]/', '', $filename);
$upload_path = GSDATAUPLOADPATH . $new_filename;
// Move uploaded file
if (move_uploaded_file($file['tmp_name'], $upload_path)) {
// Return relative URL
$image_url = '../data/uploads/' . $new_filename;
echo json_encode(array(
'success' => true,
'filename' => $new_filename,
'url' => $image_url,
'message' => i18n_r(TC_PLUGIN_ID . '/MSG_UPLOAD_SUCCESS')
));
} else {
echo json_encode(array(
'success' => false,
'message' => i18n_r(TC_PLUGIN_ID . '/ERROR_SAVE_PATH') . ' ' . GSDATAUPLOADPATH
));
}
exit;
}
/**
* Main Router - handles subpages
*/
function tc_main_router() {
// Help page
if (isset($_GET['help'])) {
include(TC_PLUGIN_PATH . 'help.inc.php');
return;
}
// Default - Customizer page
include(TC_PLUGIN_PATH . 'customizer.inc.php');
}
/**
* Helper Functions for Templates
*/
function tc_get_customizer() {
global $tc_customizer_instance;
if ($tc_customizer_instance === null) {
$tc_customizer_instance = TemplateCustomizer::getInstance();
}
return $tc_customizer_instance;
}
function tc_get_option($id, $default = '') {
return tc_get_customizer()->getSetting($id, $default);
}
function tc_set_option($id, $value) {
return tc_get_customizer()->setSetting($id, $value);
}
function tc_add_panel($id, $args = array()) {
return tc_get_customizer()->addPanel($id, $args);
}
function tc_add_section($id, $args = array()) {
return tc_get_customizer()->addSection($id, $args);
}
function tc_add_setting($id, $args = array()) {
return tc_get_customizer()->addSetting($id, $args);
}
function tc_add_control($id, $args = array()) {
return tc_get_customizer()->addControl($id, $args);
}
function tc_remove($type, $id) {
return tc_get_customizer()->remove($type, $id);
}
/**
* Get active template name
*/
function tc_get_active_template() {
global $TEMPLATE;
// Try global variable first
if (isset($TEMPLATE) && !empty($TEMPLATE)) {
return $TEMPLATE;
}
// Try reading from config file
$settings_file = GSDATAOTHERPATH . 'website.xml';
if (file_exists($settings_file)) {
$data = getXML($settings_file);
if (isset($data->TEMPLATE)) {
return (string)$data->TEMPLATE;
}
}
// Fallback to default
return 'Innovation';
}
/**
* AJAX handler for saving settings
*/
function tc_ajax_handler() {
// Set headers
http_response_code(200);
header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: 0');
// Validate request
if (!isset($_POST['tc_ajax_action'])) {
echo json_encode(array(
'success' => false,
'message' => i18n_r(TC_PLUGIN_ID . '/ERROR_NO_ACTION')
));
exit;
}
if (!isset($_POST['nonce']) || !check_nonce($_POST['nonce'], 'tc_live_preview')) {
echo json_encode(array(
'success' => false,
'message' => i18n_r(TC_PLUGIN_ID . '/ERROR_INVALID_TOKEN')
));
exit;
}
try {
$customizer = tc_get_customizer();
$settings = isset($_POST['settings']) ? $_POST['settings'] : array();
// Process settings
foreach ($settings as $key => $value) {
$customizer->setSetting($key, $value);
}
// Save or just preview
if ($_POST['tc_ajax_action'] === 'save_final') {
if ($customizer->save()) {
echo json_encode(array(
'success' => true,
'message' => i18n_r(TC_PLUGIN_ID . '/MSG_SAVE_SUCCESS'),
'count' => count($settings)
));
} else {
echo json_encode(array(
'success' => false,
'message' => i18n_r(TC_PLUGIN_ID . '/ERROR_SAVE_FILE')
));
}
} else {
// Just preview
echo json_encode(array(
'success' => true,
'message' => i18n_r(TC_PLUGIN_ID . '/PREVIEW_REFRESH'),
'count' => count($settings)
));
}
} catch (Exception $e) {
echo json_encode(array(
'success' => false,
'message' => i18n_r(TC_PLUGIN_ID . '/ERROR_EXCEPTION') . ': ' . $e->getMessage()
));
}
exit;
}
/**
* Render control for live preview
*/
function tc_render_control_live($id, $control) {
$customizer = tc_get_customizer();
$value = $customizer->getSetting($id, $control['settings'] ?? '');
$type = $control['type'] ?? 'text';
$label = $control['label'] ?? '';
$description = $control['description'] ?? '';
$choices = $control['choices'] ?? array();
$input_attrs = $control['input_attrs'] ?? array();
// Get default value from settings
$settings = $customizer->getSettings();
$default_value = isset($settings[$id]['default']) ? $settings[$id]['default'] : '';
echo '<div class="tc-control-item tc-control-' . $type . '" data-control-id="' . $id . '" data-default-value="' . htmlspecialchars($default_value) . '">';
if (!empty($label)) {
echo '<div class="tc-control-header">';
echo '<label class="tc-control-label">' . htmlspecialchars($label) . '</label>';
// Add reset button if value differs from default
if ($value !== $default_value && $default_value !== '') {
echo '<button type="button" class="tc-reset-btn" data-setting-id="' . $id . '" title="' . i18n_r(TC_PLUGIN_ID . '/CONTROL_RESET_TO_DEFAULT') . '">';
echo '<i class="fa fa-undo"></i>';
echo '</button>';
}
echo '</div>';
}
if (!empty($description)) {
echo '<p class="tc-control-description">' . htmlspecialchars($description) . '</p>';
}
$attrs_string = 'data-setting-id="' . $id . '" class="tc-input"';
foreach ($input_attrs as $attr => $attr_value) {
$attrs_string .= ' ' . $attr . '="' . htmlspecialchars($attr_value) . '"';
}
echo '<div class="tc-control-input">';
switch ($type) {
case 'text':
case 'email':
case 'url':
case 'number':
echo '<input type="' . $type . '" value="' . htmlspecialchars($value) . '" ' . $attrs_string . '>';
break;
case 'textarea':
echo '<textarea rows="4" ' . $attrs_string . '>' . htmlspecialchars($value) . '</textarea>';
break;
case 'checkbox':
echo '<label class="tc-checkbox-wrapper">';
echo '<input type="checkbox" value="1" ' . ($value ? 'checked' : '') . ' ' . $attrs_string . '>';
echo '<span class="tc-checkbox-slider"></span>';
echo '</label>';
break;
case 'radio':
foreach ($choices as $choice_value => $choice_label) {
echo '<label class="tc-radio-wrapper">';
echo '<input type="radio" value="' . htmlspecialchars($choice_value) . '" ' . ($value == $choice_value ? 'checked' : '') . ' ' . $attrs_string . '>';
echo '<span class="tc-radio-label">' . htmlspecialchars($choice_label) . '</span>';
echo '</label>';
}
break;
case 'select':
case 'dropdown':
echo '<select ' . $attrs_string . '>';
foreach ($choices as $choice_value => $choice_label) {
echo '<option value="' . htmlspecialchars($choice_value) . '" ' . ($value == $choice_value ? 'selected' : '') . '>' . htmlspecialchars($choice_label) . '</option>';
}
echo '</select>';
break;
case 'color':
echo '<div class="tc-color-picker">';
echo '<input type="color" value="' . htmlspecialchars($value) . '" ' . $attrs_string . '>';
echo '<input type="text" value="' . htmlspecialchars($value) . '" class="tc-color-input" readonly>';
echo '</div>';
break;
case 'page':
// Get all pages from GetSimple
$pages = array();
if (function_exists('get_available_pages')) {
global $pagesArray;
if (empty($pagesArray)) {
get_available_pages();
}
foreach ($pagesArray as $page) {
$pages[$page['url']] = $page['title'] . ' (' . $page['url'] . ')';
}
}
?>
<select class="tc-input" data-setting-id="<?php echo $control['settings']; ?>">
<option value=""><?php i18n(TC_PLUGIN_ID . '/CONTROL_SELECT_PAGE'); ?></option>
<?php foreach ($pages as $slug => $title): ?>
<option value="<?php echo htmlspecialchars($slug); ?>" <?php echo ($value === $slug) ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($title); ?>
</option>
<?php endforeach; ?>
</select>
<?php
break;
case 'range':
$min = $input_attrs['min'] ?? 0;
$max = $input_attrs['max'] ?? 100;
$step = $input_attrs['step'] ?? 1;
echo '<div class="tc-range-wrapper">';
echo '<input type="range" value="' . htmlspecialchars($value) . '" min="' . $min . '" max="' . $max . '" step="' . $step . '" ' . $attrs_string . '>';
echo '<span class="tc-range-value">' . htmlspecialchars($value) . '</span>';
echo '</div>';
break;
case 'image':
case 'media':
echo '<div class="tc-image-control">';
echo '<input type="text" value="' . htmlspecialchars($value) . '" ' . $attrs_string . ' placeholder="' . i18n_r(TC_PLUGIN_ID . '/UPLOAD_SELECT') . '">';
echo '<button type="button" class="tc-image-btn"><i class="fa fa-upload"></i> ' . i18n_r(TC_PLUGIN_ID . '/CONTROL_UPLOAD_IMAGE') . '</button>';
if (!empty($value)) {
echo '<div class="tc-image-thumb">';
echo '<img src="' . htmlspecialchars($value) . '" alt="Preview">';
echo '<button type="button" class="tc-remove-image" title="' . i18n_r(TC_PLUGIN_ID . '/CONTROL_REMOVE_IMAGE') . '"><i class="fa fa-times"></i></button>';
echo '</div>';
}
echo '</div>';
break;
}
echo '</div>'; // .tc-control-input
echo '</div>'; // .tc-control-item
}
/**
* Initialize default sections for themes
*/
add_action('index-pretemplate', 'tc_initialize_defaults');
add_action('theme-customizer-init', 'tc_initialize_defaults');
function tc_initialize_defaults() {
static $loaded = false;
if ($loaded) return;
$loaded = true;
$template = tc_get_active_template();
if (!$template) return;
$theme_customizer_file = GSTHEMESPATH . $template . '/customizer.php';
if (file_exists($theme_customizer_file)) {
include_once($theme_customizer_file);
}
}
/**
* Add floating "Edit Template" button on frontend for logged-in users
*/
add_action('theme-header', 'tc_add_floating_edit_button');
function tc_add_floating_edit_button() {
// Only show if user is logged in
if (!cookie_check()) {
return;
}
// Hide in admin panel
if (defined('IN_GS_ADMIN') || strpos($_SERVER['REQUEST_URI'], '/admin/') !== false) {
return;
}
// Hide in preview mode (customizer iframe)
if (isset($_GET['tc_preview']) && $_GET['tc_preview'] == '1') {
return;
}
// Get site URL and plugin ID
global $SITEURL;
$customizer_url = $SITEURL . 'admin/load.php?id=' . TC_PLUGIN_ID;
?>
<style>
.tc-floating-edit-btn {
position: fixed;
left: 20px;
top: 50%;
transform: translateY(-50%);
z-index: 99999;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: #fff;
border: none;
border-radius: 30px;
padding: 15px 20px;
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
cursor: pointer;
font-size: 14px;
font-weight: 600;
text-decoration: none;
display: flex;
align-items: center;
gap: 10px;
transition: all 0.3s ease;
writing-mode: vertical-rl;
text-orientation: mixed;
}
.tc-floating-edit-btn:hover {
transform: translateY(-50%) translateX(-5px);
box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6);
background: linear-gradient(135deg, #764ba2 0%, #667eea 100%);
}
.tc-floating-edit-btn i {
font-size: 16px;
}
@media (max-width: 768px) {
.tc-floating-edit-btn {
left: 10px;
padding: 12px 15px;
font-size: 12px;
}
}
</style>
<a href="<?php echo $customizer_url; ?>" class="tc-floating-edit-btn" title="<?php i18n(TC_PLUGIN_ID . '/BTN_EDIT_TEMPLATE'); ?>">
<i class="fa fa-paint-brush"></i>
<span><?php i18n(TC_PLUGIN_ID . '/BTN_EDIT_TEMPLATE'); ?></span>
</a>
<?php
}
/**
* Restore scroll position in preview mode
*/
add_action('content-top', 'tc_restore_scroll_position');
function tc_restore_scroll_position() {
if (isset($_GET['tc_preview']) && $_GET['tc_preview'] == '1') {
$scroll_pos = isset($_GET['tc_scroll']) ? intval($_GET['tc_scroll']) : 0;
?>
<script>
(function() {
var scrollPos = <?php echo $scroll_pos; ?>;
console.log('TC: Should scroll to:', scrollPos);
function doScroll() {
if (scrollPos > 0) {
window.scrollTo(0, scrollPos);
document.documentElement.scrollTop = scrollPos;
document.body.scrollTop = scrollPos;
console.log('TC: Scrolled to:', scrollPos);
console.log('TC: Current scroll:', window.scrollY);
}
}
// Try immediately
doScroll();
// Try on DOMContentLoaded
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', doScroll);
}
// Try on window load (final attempt)
window.addEventListener('load', function() {
setTimeout(doScroll, 100);
});
})();
</script>
<?php
}
}