<?php
/**
 * WP Multi – Modul: Custom Text Box
 *
 * Automatisch aus wp-multi.php ausgelagert. Wird über den Loader in
 * wp-multi.php eingebunden (nur wenn das WP Multi Toolkit aktiv ist).
 */

if (!defined('ABSPATH')) { exit; }

/*
 * Custom Text Box
 */

// Funktion zur Registrierung der zusätzlichen Einstellungen für benutzerdefinierte Texte
function wp_multi_custom_text_register_settings() {
    add_option('wp_multi_custom_texts', '');
    add_option('wp_multi_second_custom_text', '');
    add_option('wp_multi_enable_custom_texts', '1');
    register_setting('wp_multi_custom_text_options_group', 'wp_multi_custom_texts');
    register_setting('wp_multi_custom_text_options_group', 'wp_multi_second_custom_text');
    register_setting('wp_multi_custom_text_options_group', 'wp_multi_enable_custom_texts');
}
add_action('admin_init', 'wp_multi_custom_text_register_settings');

// Der Custom-Text-Menüpunkt wird zentral in wp_multi_register_admin_menus() registriert.

// Funktion zum Erstellen der Einstellungsseite
function wp_multi_custom_text_settings_page_content() {
    wpmt_admin_page_open(
        __('Custom Text', 'wp-multi'),
        __('Vordefinierte Textboxen am Ende deiner Beiträge', 'wp-multi')
    );

    wpmt_admin_card_open();
    ?>
    <form method="post" action="options.php">
        <?php settings_fields('wp_multi_custom_text_options_group'); ?>
        <table class="form-table">
            <tr valign="top">
                <th scope="row"><?php _e('Custom Texte aktivieren', 'wp-multi'); ?></th>
                <td>
                    <input type="checkbox" name="wp_multi_enable_custom_texts" value="1" <?php checked(1, get_option('wp_multi_enable_custom_texts'), true); ?> />
                    <p class="description"><?php _e('Aktiviere oder deaktiviere die Anzeige der benutzerdefinierten Texte auf der Webseite.', 'wp-multi'); ?></p>
                </td>
            </tr>
            <tr valign="top">
                <th scope="row"><?php _e('Custom Texte', 'wp-multi'); ?></th>
                <td>
                    <textarea name="wp_multi_custom_texts" rows="10" cols="50" class="large-text"><?php echo esc_textarea(get_option('wp_multi_custom_texts')); ?></textarea>
                    <p class="description"><?php _e('Gib jeden Text in einer neuen Zeile ein.', 'wp-multi'); ?></p>
                </td>
            </tr>
            <tr valign="top">
                <th scope="row"><?php _e('Zweiter Custom Text', 'wp-multi'); ?></th>
                <td>
                    <textarea name="wp_multi_second_custom_text" rows="10" cols="50" class="large-text"><?php echo esc_textarea(get_option('wp_multi_second_custom_text')); ?></textarea>
                    <p class="description"><?php _e('Gib den zweiten Custom Text ein, der über der ersten Box angezeigt wird.', 'wp-multi'); ?></p>
                </td>
            </tr>
        </table>
        <?php submit_button(); ?>
    </form>
    <?php
    wpmt_admin_card_close();

    wpmt_admin_page_close();
}

// Anzeige der Custom Texts in einer Box im Frontend
function wp_multi_custom_text_display($content) {
    $enable_custom_texts = get_option('wp_multi_enable_custom_texts', '1');
    if ($enable_custom_texts !== '1') {
        return $content;
    }

    if (is_single()) {
        global $post;
        $guest_author_name = get_post_meta($post->ID, '_guest_author', true);
        $author_name = !empty($guest_author_name) ? $guest_author_name : get_the_author();
        $custom_texts = get_option('wp_multi_custom_texts', '');
        $second_custom_texts = get_option('wp_multi_second_custom_text', '');
        $custom_texts_array = array_filter(array_map('trim', explode("\n", $custom_texts)));
        $second_custom_texts_array = array_filter(array_map('trim', explode("\n", $second_custom_texts)));
        $selected_custom_text = get_post_meta($post->ID, '_custom_text_choice', true);
        $selected_second_custom_text = get_post_meta($post->ID, '_second_custom_text_choice', true);

        if (empty($custom_texts_array) && empty($second_custom_texts_array)) {
            return $content;
        }

        $output = '<div class="custom-text-box" style="margin-top: 40px; padding: 20px; background-color: #f0f0f0; border: 2px solid #ddd; border-radius: 10px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); max-width: 400px; width: auto; font-size: 16px; line-height: 1.2; clear: both; margin-left: auto; margin-right: 0; display: block; margin-bottom: 20px; position: relative;">';
        $output .= '<p><strong>' . __('Autor:', 'wp-multi') . ' ' . esc_html($author_name);
        if ($selected_second_custom_text !== '' && isset($second_custom_texts_array[$selected_second_custom_text])) {
            $output .= '  |  ' . esc_html($second_custom_texts_array[$selected_second_custom_text]);
        }
        $output .= '</strong></p>';
        if ($selected_custom_text !== '' && isset($custom_texts_array[$selected_custom_text])) {
            $output .= '<p><em>' . esc_html($custom_texts_array[$selected_custom_text]) . '</em></p>';
        }
        $output .= '</div>';

        return $content . $output;
    }
    return $content;
}
add_filter('the_content', 'wp_multi_custom_text_display');

// Funktion zum Hinzufügen der Meta-Box für beide Custom Texts
function wp_multi_add_custom_text_fields($post) {
    $custom_texts = get_option('wp_multi_custom_texts');
    $custom_texts_array = array_filter(array_map('trim', explode("\n", $custom_texts)));
    $second_custom_text = get_option('wp_multi_second_custom_text');
    $second_custom_text_array = array_filter(array_map('trim', explode("\n", $second_custom_text)));
    $selected_custom_text = get_post_meta($post->ID, '_custom_text_choice', true);
    $selected_second_custom_text = get_post_meta($post->ID, '_second_custom_text_choice', true);
    wp_nonce_field('wp_multi_custom_text_choice_nonce', 'wp_multi_custom_text_choice_nonce_field');
    ?>
    <label for="custom_text_choice"><?php _e('Wähle den Custom Text (unterer Bereich):', 'wp-multi'); ?></label>
    <select name="custom_text_choice" id="custom_text_choice" class="widefat">
        <option value=""><?php _e('Keinen Text auswählen', 'wp-multi'); ?></option>
        <?php foreach ($custom_texts_array as $key => $value) { ?>
            <option value="<?php echo esc_attr($key); ?>" <?php selected($selected_custom_text, $key); ?>><?php echo esc_html($value); ?></option>
        <?php } ?>
    </select>
    <label for="second_custom_text_choice"><?php _e('Wähle den zweiten Custom Text (oberer Bereich):', 'wp-multi'); ?></label>
    <select name="second_custom_text_choice" id="second_custom_text_choice" class="widefat">
        <option value=""><?php _e('Keinen Text auswählen', 'wp-multi'); ?></option>
        <?php foreach ($second_custom_text_array as $key => $value) { ?>
            <option value="<?php echo esc_attr($key); ?>" <?php selected($selected_second_custom_text, $key); ?>><?php echo esc_html($value); ?></option>
        <?php } ?>
    </select>
    <?php
}

// Meta-Box hinzufügen
add_action('add_meta_boxes', function() {
    add_meta_box('wp_multi_custom_text', __('Custom Text Auswahl', 'wp-multi'), 'wp_multi_add_custom_text_fields', 'post', 'normal', 'high');
});

// Speichern der benutzerdefinierten Textauswahl im Beitrag
function wp_multi_save_custom_text_choice($post_id) {
    if (!isset($_POST['wp_multi_custom_text_choice_nonce_field']) || !wp_verify_nonce($_POST['wp_multi_custom_text_choice_nonce_field'], 'wp_multi_custom_text_choice_nonce')) {
        return;
    }
    if (!current_user_can('edit_post', $post_id)) {
        return;
    }
    if (isset($_POST['custom_text_choice'])) {
        update_post_meta($post_id, '_custom_text_choice', sanitize_text_field(wp_unslash($_POST['custom_text_choice'])));
    }
    if (isset($_POST['second_custom_text_choice'])) {
        update_post_meta($post_id, '_second_custom_text_choice', sanitize_text_field(wp_unslash($_POST['second_custom_text_choice'])));
    }
}
add_action('save_post', 'wp_multi_save_custom_text_choice');

