200 lines
6.2 KiB
PHP
200 lines
6.2 KiB
PHP
<?php
|
|
/**
|
|
* Plugin Name: Skrift Konfigurator
|
|
* Description: Interaktiver Konfigurator für handgeschriebene Briefe
|
|
* Version: 0.3.0
|
|
* Author: Skrift
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) { exit; }
|
|
|
|
// Admin Settings IMMER laden (für REST API Permission Callbacks)
|
|
require_once plugin_dir_path(__FILE__) . 'includes/admin-settings.php';
|
|
|
|
// Gutscheine IMMER laden (für REST API)
|
|
require_once plugin_dir_path(__FILE__) . 'includes/admin-vouchers.php';
|
|
|
|
// Bestellnummern-Verwaltung laden (für REST API)
|
|
require_once plugin_dir_path(__FILE__) . 'includes/admin-orders.php';
|
|
|
|
// Backend API Proxy laden (für REST API)
|
|
require_once plugin_dir_path(__FILE__) . 'includes/api-proxy.php';
|
|
|
|
final class Skrift_Konfigurator_Plugin {
|
|
|
|
const VERSION = '0.3.0';
|
|
const SLUG = 'skrift-konfigurator';
|
|
|
|
public function __construct() {
|
|
add_action('wp_enqueue_scripts', [$this, 'register_assets']);
|
|
add_filter('script_loader_tag', [$this, 'add_module_attribute'], 10, 3);
|
|
add_shortcode('skrift_konfigurator', [$this, 'render_shortcode']);
|
|
add_shortcode('skrift_preisrechner', [$this, 'render_preisrechner_shortcode']);
|
|
}
|
|
|
|
public function register_assets(): void {
|
|
$base = plugin_dir_url(__FILE__);
|
|
|
|
wp_register_style(
|
|
self::SLUG,
|
|
$base . 'assets/css/configurator.css',
|
|
[],
|
|
self::VERSION
|
|
);
|
|
|
|
wp_register_script(
|
|
self::SLUG,
|
|
$base . 'assets/js/configurator-app.js',
|
|
[],
|
|
self::VERSION,
|
|
true
|
|
);
|
|
|
|
// Preisrechner Script
|
|
wp_register_script(
|
|
'skrift-preisrechner',
|
|
$base . 'assets/js/price-calculator.js',
|
|
[],
|
|
self::VERSION,
|
|
true
|
|
);
|
|
}
|
|
|
|
public function add_module_attribute(string $tag, string $handle, string $src): string {
|
|
// Beide Scripts als ES6-Module laden
|
|
if ($handle !== self::SLUG && $handle !== 'skrift-preisrechner') return $tag;
|
|
return '<script type="module" src="' . esc_url($src) . '"></script>';
|
|
}
|
|
|
|
public function render_shortcode($atts = []): string {
|
|
wp_enqueue_style(self::SLUG);
|
|
wp_enqueue_script(self::SLUG);
|
|
|
|
// Einstellungen aus DB holen
|
|
$settings = Skrift_Konfigurator_Admin_Settings::get_settings();
|
|
$vouchers = Skrift_Konfigurator_Vouchers::get_vouchers();
|
|
|
|
// WICHTIG: Sicherstellen dass leere Arrays als Objekt {} encodiert werden, nicht als Array []
|
|
if (empty($vouchers)) {
|
|
$vouchers = new stdClass();
|
|
}
|
|
|
|
// PayPal-Einstellungen für Frontend vorbereiten (ohne sensible Daten)
|
|
$paypal_frontend = [];
|
|
if (!empty($settings['paypal']['enabled'])) {
|
|
$mode = $settings['paypal']['mode'] ?? 'sandbox';
|
|
$client_id = ($mode === 'live')
|
|
? ($settings['paypal']['client_id_live'] ?? '')
|
|
: ($settings['paypal']['client_id_sandbox'] ?? '');
|
|
|
|
$paypal_frontend = [
|
|
'enabled' => true,
|
|
'mode' => $mode,
|
|
'client_id' => $client_id,
|
|
];
|
|
}
|
|
|
|
ob_start();
|
|
?>
|
|
<script>
|
|
<?php
|
|
// Settings kopieren aber sensible Daten entfernen
|
|
$frontend_settings = $settings;
|
|
// API Token NICHT ans Frontend senden - wird über WordPress Proxy gehandhabt
|
|
unset($frontend_settings['backend_connection']['api_token']);
|
|
// API Security Key NICHT ans Frontend senden
|
|
unset($frontend_settings['api_security']);
|
|
// PayPal Secrets NICHT ans Frontend senden
|
|
unset($frontend_settings['paypal']['client_secret_sandbox']);
|
|
unset($frontend_settings['paypal']['client_secret_live']);
|
|
?>
|
|
window.SkriftConfigurator = <?php echo wp_json_encode([
|
|
'version' => self::VERSION,
|
|
'restUrl' => esc_url_raw(rest_url()),
|
|
'nonce' => wp_create_nonce('wp_rest'),
|
|
'apiKey' => $settings['api_security']['api_key'] ?? '', // API Key für REST-Aufrufe
|
|
'settings' => $frontend_settings,
|
|
'vouchers' => $vouchers,
|
|
'paypal' => $paypal_frontend,
|
|
]); ?>;
|
|
</script>
|
|
<style>.sk-configurator{opacity:0;transition:opacity .2s ease}.sk-configurator.sk-ready{opacity:1}</style>
|
|
<div class="sk-configurator" data-skrift-konfigurator="1">
|
|
<div class="sk-configurator__layout">
|
|
|
|
<!-- Main Content Area -->
|
|
<div class="sk-main">
|
|
|
|
<!-- Top Bar mit Preis und Stepper -->
|
|
<div id="sk-topbar" class="sk-topbar"></div>
|
|
|
|
<!-- Stepper -->
|
|
<div id="sk-stepper"></div>
|
|
|
|
<!-- Form Content -->
|
|
<div id="sk-form"></div>
|
|
|
|
<!-- Mobile Preview (nur auf mobilen Geräten sichtbar, vor dem Button) -->
|
|
<div id="sk-preview-mobile" class="sk-preview-mobile"></div>
|
|
|
|
<!-- Navigation Buttons -->
|
|
<div class="sk-nav">
|
|
<button type="button" id="sk-prev" class="sk-btn sk-btn-secondary">
|
|
← Zurück
|
|
</button>
|
|
<button type="button" id="sk-next" class="sk-btn sk-btn-primary">
|
|
Weiter →
|
|
</button>
|
|
</div>
|
|
|
|
<!-- Mobile Contact Card (nach dem Button, nur mobile) -->
|
|
<div id="sk-contact-mobile" class="sk-contact-card-mobile"></div>
|
|
|
|
</div>
|
|
|
|
<!-- Preview Sidebar -->
|
|
<aside class="sk-side">
|
|
<div id="sk-preview"></div>
|
|
</aside>
|
|
|
|
</div>
|
|
</div>
|
|
<?php
|
|
return ob_get_clean();
|
|
}
|
|
|
|
/**
|
|
* Rendert den Preisrechner Shortcode
|
|
*/
|
|
public function render_preisrechner_shortcode($atts = []): string {
|
|
wp_enqueue_style(self::SLUG);
|
|
wp_enqueue_script('skrift-preisrechner');
|
|
|
|
// Einstellungen aus DB holen (gleiche wie Konfigurator)
|
|
$settings = Skrift_Konfigurator_Admin_Settings::get_settings();
|
|
|
|
ob_start();
|
|
?>
|
|
<script>
|
|
<?php
|
|
// Settings kopieren aber sensible Daten entfernen
|
|
$frontend_settings = $settings;
|
|
unset($frontend_settings['backend_connection']['api_token']);
|
|
unset($frontend_settings['api_security']);
|
|
unset($frontend_settings['paypal']);
|
|
?>
|
|
window.SkriftPreisrechner = <?php echo wp_json_encode([
|
|
'version' => self::VERSION,
|
|
'settings' => $frontend_settings,
|
|
]); ?>;
|
|
</script>
|
|
<div class="sk-configurator" data-skrift-preisrechner="1">
|
|
<!-- Preisrechner wird per JavaScript gerendert -->
|
|
</div>
|
|
<?php
|
|
return ob_get_clean();
|
|
}
|
|
|
|
}
|
|
|
|
new Skrift_Konfigurator_Plugin();
|