<?php
/*
 * Amazon Price Tracker with Google Charts and Price Alert Function
 *
 * This script creates and manages a price tracking system for Amazon products. 
 * It provides the following functionalities:
 *
 * - Database tables for storing price alerts and price history.
 * - A scheduled task (Cron Job) that checks price changes every 6 hours.
 * - Price alerts that notify users via email when their target price is reached.
 * - A Google Charts-based price history display.
 * - A shortcode [boxyfy_preiswecker] that automatically integrates the price tracker into WordPress pages.
 *
 * @package Boxyfy
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

/* -------------------------------------------------
   1) Tabellen erstellen
--------------------------------------------------- */
function boxyfy_preiswecker_create_table() {
    global $wpdb;

    // Haupttabelle für Preiswecker-Einträge
    $table_name = $wpdb->prefix . 'boxyfy_preiswecker_eintraege';
    $charset_collate = $wpdb->get_charset_collate();
    $sql = "CREATE TABLE IF NOT EXISTS $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        product_id bigint(20) NOT NULL,
        target_price float NOT NULL,
        email varchar(255) NOT NULL,
        notified tinyint(1) DEFAULT 0 NOT NULL,
        created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
        PRIMARY KEY  (id)
    ) $charset_collate;";
    require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    dbDelta($sql);

    // Historientabelle für Preisentwicklungen
    $history_table = $wpdb->prefix . 'boxyfy_preiswecker_price_history';
    $sql2 = "CREATE TABLE IF NOT EXISTS $history_table (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        product_id bigint(20) NOT NULL,
        price float NOT NULL,
        recorded_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL,
        PRIMARY KEY (id)
    ) $charset_collate;";
    dbDelta($sql2);
}
register_activation_hook(__FILE__, 'boxyfy_preiswecker_create_table');

/* -------------------------------------------------
   2) Tabellen-Überprüfung im Admin-Bereich
--------------------------------------------------- */
function boxyfy_preiswecker_check_tables() {
    global $wpdb;
    
    $table1 = $wpdb->prefix . 'boxyfy_preiswecker_eintraege';
    $table2 = $wpdb->prefix . 'boxyfy_preiswecker_price_history';
    
    require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    
    // Überprüfe Haupttabelle mit Caching
    $cache_key1 = 'boxyfy_preiswecker_table1_exists';
    $table1_exists = wp_cache_get($cache_key1, 'boxyfy_preiswecker');
    if ( false === $table1_exists ) {
        $table1_exists = $wpdb->get_var("SHOW TABLES LIKE '$table1'") == $table1;
        wp_cache_set($cache_key1, $table1_exists, 'boxyfy_preiswecker', 300);
    }
    if ( ! $table1_exists ) {
        boxyfy_preiswecker_create_table();
    }
    
    // Überprüfe Historientabelle mit Caching
    $cache_key2 = 'boxyfy_preiswecker_table2_exists';
    $table2_exists = wp_cache_get($cache_key2, 'boxyfy_preiswecker');
    if ( false === $table2_exists ) {
        $table2_exists = $wpdb->get_var("SHOW TABLES LIKE '$table2'") == $table2;
        wp_cache_set($cache_key2, $table2_exists, 'boxyfy_preiswecker', 300);
    }
    if ( ! $table2_exists ) {
        boxyfy_preiswecker_create_table();
    }
}
add_action('admin_init', 'boxyfy_preiswecker_check_tables');

/* -------------------------------------------------
   3) Cron-Intervall (alle 6h)
--------------------------------------------------- */
function boxyfy_preiswecker_add_cron_interval($schedules) {
    $schedules['every_6_hours'] = [
        'interval' => 6 * 60 * 65, // 6.5 Stunden in Sekunden
        'display'  => __('Alle 6,5 Stunden', 'boxyfy')
    ];
    return $schedules;
}
add_filter('cron_schedules', 'boxyfy_preiswecker_add_cron_interval');

/* -------------------------------------------------
   4) Cron-Job planen
--------------------------------------------------- */
if ( ! wp_next_scheduled('boxyfy_productpage_preiswecker_update') ) {
    wp_schedule_event(time(), 'every_6_hours', 'boxyfy_productpage_preiswecker_update');
}
add_action('boxyfy_productpage_preiswecker_update', 'boxyfy_preiswecker_check_all_entries');

/* -------------------------------------------------
   5) Alle Preiswecker-Einträge prüfen und Preis-Historie aktualisieren
--------------------------------------------------- */
function boxyfy_preiswecker_check_all_entries() {
	global $wpdb;
	$history_table = $wpdb->prefix . 'boxyfy_preiswecker_price_history';
	$alerts_table  = $wpdb->prefix . 'boxyfy_preiswecker_eintraege';

	$cache_key = 'boxyfy_preiswecker_distinct_products';
	$entries = wp_cache_get($cache_key, 'boxyfy_preiswecker');
	if ( false === $entries ) {
		$entries = $wpdb->get_results("SELECT DISTINCT product_id FROM $history_table");
		wp_cache_set($cache_key, $entries, 'boxyfy_preiswecker', 300);
	}

	if ( empty( $entries ) ) {
		return;
	}

	foreach ( $entries as $entry ) {
		$product_id = $entry->product_id;

		$raw_price = get_post_meta( $product_id, 'tsprodukt_price1', true );

		if ( empty( $raw_price ) ) {
			continue;
		}

		$formatted_price = preg_replace('/[^\d,\.]/', '', $raw_price);
		$formatted_price = str_replace('.', '', $formatted_price);
		$formatted_price = str_replace(',', '.', $formatted_price);
		$current_price = floatval($formatted_price);

		if ( $current_price <= 0 ) {
			continue;
		}

		$cache_key_last = 'boxyfy_preiswecker_last_price_' . $product_id;
		$last_price = wp_cache_get( $cache_key_last, 'boxyfy_preiswecker' );
		if ( false === $last_price ) {
			$last_price = $wpdb->get_var( $wpdb->prepare(
				"SELECT price FROM $history_table WHERE product_id = %d ORDER BY recorded_at DESC LIMIT 1",
				$product_id
			) );
			wp_cache_set( $cache_key_last, $last_price, 'boxyfy_preiswecker', 300 );
		}

		if ( $last_price === null || floatval( $last_price ) !== $current_price ) {
			$wpdb->insert(
				$history_table,
				[
					'product_id'  => $product_id,
					'price'       => $current_price,
					'recorded_at' => current_time( 'mysql' ),
				],
				[ '%d', '%f', '%s' ]
			);

			wp_cache_delete( $cache_key_last, 'boxyfy_preiswecker' );
		}

		$additional_prices = get_post_meta($product_id, 'boxyfy_weitere_preise', true);
		$alle_preise = [$current_price];
		if (!empty($additional_prices)) {
			foreach ($additional_prices as $preis_feld) {
				if (isset($preis_feld['preis'])) {
					$p = preg_replace('/[^\d,\.]/', '', $preis_feld['preis']);
					$p = str_replace('.', '', $p);
					$p = str_replace(',', '.', $p);
					$alle_preise[] = floatval($p);
				}
			}
		}

		$alle_preise = array_filter($alle_preise, fn($p) => $p > 0);
		$lowest_price = !empty($alle_preise) ? min($alle_preise) : $current_price;

		$cache_key_alerts = 'boxyfy_preiswecker_alerts_' . $product_id;
		$price_entries = wp_cache_get( $cache_key_alerts, 'boxyfy_preiswecker' );
		if ( false === $price_entries ) {
			$price_entries = $wpdb->get_results( $wpdb->prepare(
				"SELECT * FROM $alerts_table WHERE product_id = %d AND notified = 0",
				$product_id
			) );
			wp_cache_set( $cache_key_alerts, $price_entries, 'boxyfy_preiswecker', 300 );
		}

		foreach ( $price_entries as $price_entry ) {
			if ($lowest_price <= floatval($price_entry->target_price)) {
				boxyfy_preiswecker_check_and_notify(
					$product_id,
					$price_entry->target_price,
					$price_entry->email,
					$lowest_price
				);
			}
		}
	}
}

/* -------------------------------------------------
   6) Preisprüfung & Benachrichtigung
--------------------------------------------------- */
function boxyfy_preiswecker_check_and_notify($product_id, $target_price, $email, $lowest_price = null)
{
    global $wpdb;

    if ($lowest_price === null) {
        $raw_price = get_post_meta($product_id, 'tsprodukt_price1', true);
        $raw_price = str_replace('.', '', $raw_price);
        $raw_price = str_replace(',', '.', $raw_price);
        $current_amazon_price = floatval($raw_price);

        $additional_prices = get_post_meta($product_id, 'boxyfy_weitere_preise', true);
        $prices = [$current_amazon_price];

        if (!empty($additional_prices)) {
            foreach ($additional_prices as $preis_feld) {
                if (isset($preis_feld['preis'])) {
                    $raw_additional_price = $preis_feld['preis'];
                    $raw_additional_price = str_replace('.', '', $raw_additional_price);
                    $raw_additional_price = str_replace(',', '.', $raw_additional_price);
                    $prices[] = floatval($raw_additional_price);
                }
            }
        }

        $prices = array_filter($prices, fn($p) => $p > 0);
        $lowest_price = min($prices);
    }

    if ($lowest_price <= floatval($target_price)) {
        $subject = __("Preiswecker: Dein Zielpreis wurde erreicht!", 'boxyfy');
        $message = __("Hallo,\n\nDein Zielpreis von ", 'boxyfy') .
            number_format($target_price, 2, ',', '.') . __(" € wurde erreicht! Der aktuell niedrigste Preis beträgt: ", 'boxyfy') .
            number_format($lowest_price, 2, ',', '.') . __(" €.\n\nBesuche die folgende Produktseite:\n\n", 'boxyfy') .
            get_permalink($product_id);
        $headers = [
            'From: Dein Preiswecker <no-reply@' . wp_parse_url(home_url(), PHP_URL_HOST) . '>',
            'Content-Type: text/plain; charset=UTF-8'
        ];

        wp_mail($email, $subject, $message, $headers);

        $wpdb->delete(
            $wpdb->prefix . 'boxyfy_preiswecker_eintraege',
            ['product_id' => $product_id, 'email' => $email],
            ['%d', '%s']
        );

        wp_cache_delete('boxyfy_preiswecker_alerts_' . $product_id, 'boxyfy_preiswecker');
    }
}

/* -------------------------------------------------
   7) Historie sichern (bei Shortcode-Aufruf)
--------------------------------------------------- */
function boxyfy_preiswecker_ensure_initial_history($product_id) {
    global $wpdb;
    $history_table = $wpdb->prefix . 'boxyfy_preiswecker_price_history';

    $exists = $wpdb->get_var($wpdb->prepare(
        "SELECT COUNT(*) FROM $history_table WHERE product_id = %d",
        $product_id
    ));

    if (!$exists) {
        $current_price = boxyfy_preiswecker_fetch_amazon_price($product_id);
        if ($current_price !== false && floatval($current_price) >= 0) {
            $wpdb->insert(
                $history_table,
                [
                    'product_id'  => $product_id,
                    'price'       => $current_price,
                    'recorded_at' => current_time('mysql'),
                ],
                ['%d', '%f', '%s']
            );
            wp_cache_delete('boxyfy_preiswecker_history_' . $product_id, 'boxyfy_preiswecker');
        }
    }
}

/* -------------------------------------------------
   8) Amazon-Preis abrufen (Platzhalterfunktion)
--------------------------------------------------- */
function boxyfy_preiswecker_fetch_amazon_price($product_id) {
    $cache_key = 'boxyfy_preiswecker_amazon_price_' . $product_id;
    $cached_price = wp_cache_get($cache_key, 'boxyfy_preiswecker');
    if ( false !== $cached_price ) {
        return $cached_price;
    }
    $raw_price = get_post_meta($product_id, 'tsprodukt_price1', true);
    if (empty($raw_price)) {
        return false;
    }
    $raw_price = str_replace('.', '', $raw_price);
    $raw_price = str_replace(',', '.', $raw_price);
    $current_price = floatval($raw_price);
    if ($current_price < 0) {
        $current_price = 0;
    }
    wp_cache_set($cache_key, $current_price, 'boxyfy_preiswecker', 300);
    return $current_price;
}

/* -------------------------------------------------
   9) Google Charts anzeigen (Überarbeitet)
--------------------------------------------------- */
function boxyfy_preiswecker_display_history_chart($product_id) {
    if (function_exists('boxyfy_is_license_valid')) {
        if (!boxyfy_is_license_valid()) {
            echo '<p class="preiswecker-license-error">' . esc_html__('Keine Preiswecker-Anzeige möglich. Bitte den Lizenzschlüssel-Status überprüfen. Preiswecker ggf. deaktivieren.', 'boxyfy') . '</p>';
            return;
        }
    }
	
    global $wpdb;
    $history_table = $wpdb->prefix . 'boxyfy_preiswecker_price_history';
    
    // Historie mit Caching (5 Minuten)
    $cache_key = 'boxyfy_preiswecker_history_' . $product_id;
    $entries = wp_cache_get($cache_key, 'boxyfy_preiswecker');
    if ( false === $entries ) {
        $entries = $wpdb->get_results($wpdb->prepare(
            "SELECT price, recorded_at
             FROM $history_table
             WHERE product_id = %d
             ORDER BY recorded_at ASC",
            $product_id
        ));
        wp_cache_set($cache_key, $entries, 'boxyfy_preiswecker', 300);
    }

    if (empty($entries)) {
        echo '<p>' . esc_html__('Keine Historie vorhanden.', 'boxyfy') . '</p>';
        return;
    }

    $first_date = strtotime($entries[0]->recorded_at);
    $current_entry = end($entries);
    $current_price = floatval($current_entry->price);
    
    $current_date = date_i18n('d.m.Y');
    $current_time = current_time('timestamp');
    $total_days = floor(($current_time - $first_date) / (24 * 60 * 60));
    if ($total_days < 1) {
        $total_days = 1;
    }

    // Fülle für jeden Tag einen Preis
    $daily_prices = array_fill(0, $total_days + 1, null);
    foreach ($entries as $entry) {
        $recorded_time = strtotime($entry->recorded_at);
        $day_number = floor(($recorded_time - $first_date) / (24 * 60 * 60));
        if ($day_number < 0) $day_number = 0;
        if ($day_number > $total_days) $day_number = $total_days;
        $daily_prices[$day_number] = floatval($entry->price);
    }
    for ($i = 0; $i <= $total_days; $i++) {
        if ($daily_prices[$i] === null) {
            if ($i == 0) {
                $first_known_price = null;
                foreach ($daily_prices as $price) {
                    if ($price !== null) {
                        $first_known_price = $price;
                        break;
                    }
                }
                $daily_prices[$i] = $first_known_price !== null ? $first_known_price : 0;
            } else {
                $daily_prices[$i] = $daily_prices[$i - 1];
            }
        }
    }

    $chart_data = [];
    foreach ($daily_prices as $i => $price) {
        $day_date = strtotime("+$i days", $first_date);
        $day_date_formatted = date_i18n('d.m.Y', $day_date);
        $tooltip = "Preis: " . number_format($price, 2, ',', '.') . " €\n" . "Datum: " . $day_date_formatted;
        $chart_data[] = [
            gmdate('Y-m-d', $day_date),
            $price,
            $tooltip
        ];
    }
    $prices_array = array_column($chart_data, 1);
    $min_price = min($prices_array);
    $max_price = max($prices_array);
    $min_scaled = floor($min_price * 0.85);
    $max_scaled = ceil($max_price * 1.15);
    if ($min_scaled < 0) {
        $min_scaled = 0;
    }
    $chart_data_json = json_encode($chart_data);

    // Dynamischen Chartlinien-Farbwert abrufen
    $product_price_alert_maincolor = get_option('boxyfy_preiswecker_farben', '#0173ab');

    // Diese Daten sowie die Skalierung werden über JS (extern in preistracker-single.js) für den Chart genutzt.
    wp_localize_script('preistracker-single', 'boxyfy_preiswecker_chart_args', array(
        'chartData' => $chart_data_json,
        'minScaled' => $min_scaled,
        'maxScaled' => $max_scaled,
        'currentPrice' => number_format($current_price, 2, ',', '.'),
        'currentDate' => $current_date,
        'productPriceAlertMainColor'  => $product_price_alert_maincolor,
    ));
    
    ?>
    <div id="preiswecker_chart_container">
        <h3><?php esc_html_e('Amazon-Preisverlauf', 'boxyfy'); ?></h3>
        <div id="preiswecker_chart"></div>
        <div id="preiswecker_current_price">
            <?php echo esc_html( number_format($current_price, 2, ',', '.') ) . ' €'; ?>
 <br>
            <span><?php echo esc_html($current_date); ?></span>
        </div>
		<div class="boxyfy-preiswecker-svg-weeks-info">
			<svg width="15px" height="15px" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" id="chart-colum" class="icon glyph"><path d="M7,12v9a1,1,0,0,1-1,1H3a1,1,0,0,1-1-1V12a1,1,0,0,1,1-1H6A1,1,0,0,1,7,12ZM21,6H18a1,1,0,0,0-1,1V21a1,1,0,0,0,1,1h3a1,1,0,0,0,1-1V7A1,1,0,0,0,21,6ZM13.5,2h-3a1,1,0,0,0-1,1V21a1,1,0,0,0,1,1h3a1,1,0,0,0,1-1V3A1,1,0,0,0,13.5,2Z" style="fill:#231f20"></path>
			</svg>
        <?php
        $weeks = floor(($current_time - $first_date) / (7 * 24 * 60 * 60));
        if ($weeks < 1) {
            $weeks_text = __('Preisverlauf seit wenigen Tagen', 'boxyfy');
        } elseif ($weeks == 1) {
            $weeks_text = __('Preisverlauf seit einer Woche', 'boxyfy');
        } else {
            $weeks_text = sprintf(
                __('Preisverlauf der letzten %d Wochen', 'boxyfy'),
                $weeks
            );
        }
        echo '<div class="preiswecker_weeks">' . esc_html($weeks_text) . '</div>';
        ?>
		</div>
		<div class="preiswecker-range-buttons">
        <button class="preiswecker-range-btn" data-range="30">
            <?php esc_html_e('letzten 30 Tage', 'boxyfy'); ?>
        </button>
        <button class="preiswecker-range-btn" data-range="180">
            <?php esc_html_e('letzten 6 Monate', 'boxyfy'); ?>
        </button>
		<button class="preiswecker-range-btn" data-range="all">
        	<?php esc_html_e('gesamte Historie', 'boxyfy'); ?>
    	</button>
    </div>
    </div>
    <?php
}

/* -------------------------------------------------
   10) Formular + Diagramm im Shortcode
--------------------------------------------------- */
function boxyfy_preiswecker_shortcode($atts) {
    $atts = shortcode_atts(['id' => 0], $atts);
	$product_id = absint($atts['id']);
    if (empty($atts['id'])) return '';

    // Formularverarbeitung
	if (
    	isset($_SERVER['REQUEST_METHOD']) &&
    	'POST' === $_SERVER['REQUEST_METHOD'] &&
    	isset($_POST['preiswecker_nonce']) &&
    	wp_verify_nonce(
        	sanitize_text_field( wp_unslash($_POST['preiswecker_nonce']) ),
        	'preiswecker_form'
    	)
	) {
    	$zielpreis = isset($_POST['zielpreis']) ? floatval( sanitize_text_field( wp_unslash($_POST['zielpreis']) ) ) : 0.0;
    	$email     = isset($_POST['email']) ? sanitize_email( wp_unslash($_POST['email']) ) : '';
    	$product_id = absint($atts['id']);

        global $wpdb;
        $table_name = $wpdb->prefix . 'boxyfy_preiswecker_eintraege';

        $existing_entry = $wpdb->get_row($wpdb->prepare(
            "SELECT id FROM $table_name WHERE product_id = %d AND email = %s",
            $product_id,
            $email
        ));

        if ($existing_entry) {
            $wpdb->update(
                $table_name,
                [
                    'target_price' => $zielpreis,
                    'notified'     => 0,
                    'created_at'   => current_time('mysql')
                ],
                ['id' => $existing_entry->id],
                ['%f', '%d', '%s'],
                ['%d']
            );
            $message = __('Dein Zielpreis wurde aktualisiert', 'boxyfy');
        } else {
            $wpdb->insert(
                $table_name,
                [
                    'product_id'   => $product_id,
                    'target_price' => $zielpreis,
                    'email'        => $email,
                    'notified'     => 0,
                    'created_at'   => current_time('mysql')
                ],
                ['%d', '%f', '%s', '%d', '%s']
            );
            $message = __('Dein Preiswecker wurde gespeichert', 'boxyfy');
        }
        $nonce = wp_create_nonce('preiswecker_success_nonce');
        $redirect_url = add_query_arg(array(
            'boxyfy_preiswecker_success' => urlencode($message),
            'boxyfy_preiswecker_n'       => $nonce,
        ), get_permalink($product_id));

        wp_redirect($redirect_url);
        exit;
    }

    ob_start();

    // Erfolgsmeldung auslesen
    if (
        isset($_GET['boxyfy_preiswecker_success'], $_GET['boxyfy_preiswecker_n']) &&
        wp_verify_nonce(
            sanitize_text_field( wp_unslash($_GET['boxyfy_preiswecker_n']) ),
            'preiswecker_success_nonce'
        )
    ) {
        $message = sanitize_text_field( wp_unslash($_GET['boxyfy_preiswecker_success']) );
        echo '<div class="preiswecker-message success">' . esc_html($message) . '</div>';
    }

    // Stelle sicher, dass es mind. einen Eintrag in der Historie gibt
    boxyfy_preiswecker_ensure_initial_history($atts['id']);
    // Zeige das Diagramm
    boxyfy_preiswecker_display_history_chart($atts['id']);

    // Ausgabe des Formulars
    ?>
    <div class="preiswecker-accordion">
        <h3><strong><?php esc_html_e('Preis zu hoch?', 'boxyfy'); ?></strong></h3>
        <span>
			<?php esc_html_e('Setze jetzt einen Preisalarm für alle Anbieter-Preise.', 'boxyfy'); ?>			</span>
        <button id="preiswecker_toggle_button">
            <svg width="18px" height="18px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false">
                <path d="M5 3L2 6M22 6L19 3M6 19L4 21M18 19L20 21" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M9 13.5L11 15.5L15.5 11" stroke="#fff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M12 21C14.1217 21 16.1566 20.1571 17.6569 18.6569C19.1571 17.1566 20 15.1217 20 13C20 10.8783 19.1571 8.84344 17.6569 7.34315C16.1566 5.84285 14.1217 5 12 5C9.87827 5 7.84344 5.84285 6.34315 7.34315C4.84285 8.84344 4 10.8783 4 13C4 15.1217 4.84285 17.1566 6.34315 18.6569C7.84344 20.1571 9.87827 21 12 21Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
            <?php esc_html_e('Preiswecker setzen', 'boxyfy'); ?>
        </button>
    </div>
    <div class="preiswecker-form-container" id="preiswecker_form_container">
        <form method="post" class="preiswecker-form">
            <?php wp_nonce_field('preiswecker_form', 'preiswecker_nonce'); ?>
            <label for="zielpreis"><?php esc_html_e('Zielpreis', 'boxyfy'); ?></label>
            <input type="number" name="zielpreis" id="zielpreis" step="0.01" placeholder="<?php esc_attr_e('Zielpreis eingeben, z.B. 59,99', 'boxyfy'); ?>" required>
            <label for="email"><?php esc_html_e('E-Mail-Adresse', 'boxyfy'); ?></label>
            <input type="email" name="email" id="email" placeholder="<?php esc_attr_e('E-Mail eingeben', 'boxyfy'); ?>" required>
			<p class="boxyfy-preiswecker-hinweis">
        		<?php esc_html_e('Mit dem Absenden willigst du ein, dass Deine E-Mail und Dein Wunschpreis auf dieser Website gespeichert werden, um Dich einmalig zu benachrichtigen, sobald der Preis erreicht ist. Weitere Hinweise findest Du in unseren Datenschutzbestimmungen.', 'boxyfy'); ?>
    		</p>
            <button type="button" class="preiswecker-save-button">
                <svg width="16px" height="16px" viewBox="0 0 24 24" fill="none">
                    <path d="M21.9998 5.11V16.47C21.9998 17.92 20.9598 18.53 19.6898 17.83L17.7598 16.75C17.5998 16.66 17.4998 16.49 17.4998 16.31V8.99C17.4998 6.45 15.4298 4.38 12.8898 4.38H8.81984C8.44984 4.38 8.18984 3.99 8.35984 3.67C8.87984 2.68 9.91984 2 11.1098 2H18.8898C20.5998 2 21.9998 3.4 21.9998 5.11Z" fill="currentColor"/>
                    <path d="M12.89 5.87891H5.11C3.4 5.87891 2 7.27891 2 8.98891V20.3489C2 21.7989 3.04 22.4089 4.31 21.7089L8.24 19.5189C8.66 19.2889 9.34 19.2889 9.76 19.5189L13.69 21.7089C14.96 22.4089 16 21.7989 16 20.3489V8.98891C16 7.27891 14.6 5.87891 12.89 5.87891ZM11 12.7489H9.75V13.9989C9.75 14.4089 9.41 14.7489 9 14.7489C8.59 14.7489 8.25 14.4089 8.25 13.9989V12.7489H7C6.59 12.7489 6.25 12.4089 6.25 11.9989C6.25 11.5889 6.59 11.2489 7 11.2489H8.25V9.99891C8.25 9.58891 8.59 9.24891 9 9.24891C9.41 9.24891 9.75 9.58891 9.75 9.99891V11.2489H11C11.41 11.2489 11.75 11.5889 11.75 11.9989C11.75 12.4089 11.41 12.7489 11 12.7489Z" fill="currentColor"/>
                </svg>
                <?php esc_html_e('Preiswecker speichern', 'boxyfy'); ?>
            </button>
        </form>
    </div>
    <?php
    return ob_get_clean();
}
add_shortcode('boxyfy_preiswecker', 'boxyfy_preiswecker_shortcode');

/* -------------------------------------------------
   11) Deaktivierung
--------------------------------------------------- */
register_deactivation_hook(__FILE__, function() {
    wp_clear_scheduled_hook('boxyfy_productpage_preiswecker_update');
});