<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}


function kgv_classic_filter_auth_menu_items( $items ) {
	$is_logged_in = is_user_logged_in();

	foreach ( $items as $key => $item ) {
		$title   = isset( $item->title ) ? strtolower( wp_strip_all_tags( $item->title ) ) : '';
		$url     = isset( $item->url ) ? strtolower( $item->url ) : '';
		$classes = isset( $item->classes ) ? array_map( 'strtolower', (array) $item->classes ) : array();

		$is_login_item = in_array( 'menu-item-login', $classes, true )
			|| false !== strpos( $url, 'wp-login.php' )
			|| false !== strpos( $url, '/login' )
			|| in_array( $title, array( 'login', 'anmelden' ), true );

		$is_logout_item = in_array( 'menu-item-logout', $classes, true )
			|| false !== strpos( $url, 'action=logout' )
			|| in_array( $title, array( 'logout', 'abmelden' ), true );

		if ( $is_logged_in && $is_login_item ) {
			unset( $items[ $key ] );
		}

		if ( ! $is_logged_in && $is_logout_item ) {
			unset( $items[ $key ] );
		}
	}

	return $items;
}
add_filter( 'wp_nav_menu_objects', 'kgv_classic_filter_auth_menu_items' );

function kgv_classic_get_login_redirect_url( $requested_redirect_to = '' ) {
	$target = get_theme_mod( 'kgv_login_redirect_target', 'dashboard' );

	if ( 'home' === $target ) {
		return home_url( '/' );
	}

	if ( 'default' === $target ) {
		return ! empty( $requested_redirect_to ) ? $requested_redirect_to : admin_url();
	}

	if ( 'custom' === $target ) {
		$custom_url = trim( (string) get_theme_mod( 'kgv_login_redirect_custom_url', '' ) );

		if ( ! empty( $custom_url ) ) {
			return $custom_url;
		}
	}

	if ( current_user_can( 'manage_kleingarten' ) ) {
		return admin_url( 'admin.php?page=kgvvm-dashboard' );
	}

	if ( current_user_can( 'view_assigned_parcels' ) ) {
		return admin_url( 'admin.php?page=kgvvm-my-parcels' );
	}

	if ( current_user_can( 'read' ) ) {
		return admin_url( 'index.php' );
	}

	return home_url( '/' );
}

function kgv_classic_login_redirect_to_dashboard( $redirect_to, $requested_redirect_to, $user ) {
	if ( ! ( $user instanceof WP_User ) ) {
		return $redirect_to;
	}

	wp_set_current_user( $user->ID );

	return kgv_classic_get_login_redirect_url( $requested_redirect_to );
}
add_filter( 'login_redirect', 'kgv_classic_login_redirect_to_dashboard', 999, 3 );

function kgv_classic_add_auth_menu_item( $items, $args ) {
	if ( empty( $args->theme_location ) || 'primary' !== $args->theme_location ) {
		return $items;
	}

	if ( ! (bool) get_theme_mod( 'kgv_show_auth_menu_link', 1 ) ) {
		return $items;
	}

	$has_auth_item = false !== stripos( $items, 'wp-login.php' )
		|| false !== stripos( $items, 'action=logout' )
		|| false !== stripos( $items, 'menu-item-login' )
		|| false !== stripos( $items, 'menu-item-logout' )
		|| false !== stripos( $items, 'menu-item-auth-link' );

	if ( $has_auth_item ) {
		return $items;
	}

	if ( is_user_logged_in() ) {
		$url   = wp_logout_url( home_url( '/' ) );
		$label = __( 'Abmelden', 'kgv-classic' );
		$class = 'menu-item-auth-link is-logout';
	} else {
		$url   = wp_login_url( kgv_classic_get_login_redirect_url() );
		$label = __( 'Login', 'kgv-classic' );
		$class = 'menu-item-auth-link is-login';
	}

	$items .= sprintf(
		'<li class="menu-item %1$s"><a href="%2$s">%3$s</a></li>',
		esc_attr( $class ),
		esc_url( $url ),
		esc_html( $label )
	);

	return $items;
}
add_filter( 'wp_nav_menu_items', 'kgv_classic_add_auth_menu_item', 10, 2 );

function custom_wp_mail_from($original_email_address) {
    return 'noreply@kgv-usedom.de';
}
add_filter('wp_mail_from', 'custom_wp_mail_from');

function custom_wp_mail_from_name($original_email_from) {
    return 'Kleingartenverein Usedom e.V';
}
add_filter('wp_mail_from_name', 'custom_wp_mail_from_name');

add_action( 'admin_bar_menu', 'remove_wp_logo', 999 );
 function remove_wp_logo( $wp_admin_bar ) {
 $wp_admin_bar->remove_node( 'wp-logo' );
}

function kgv_classic_custom_logo_attributes( $custom_logo_attr ) {
	$custom_logo_attr['sizes']    = '(max-width: 782px) 40px, 56px';
	$custom_logo_attr['loading']  = 'eager';
	$custom_logo_attr['decoding'] = 'async';

	return $custom_logo_attr;
}
add_filter( 'get_custom_logo_image_attributes', 'kgv_classic_custom_logo_attributes' );

function kgv_classic_setup() {
	add_theme_support( 'title-tag' );
	add_theme_support( 'post-thumbnails' );
	add_theme_support( 'menus' );
	add_theme_support(
		'custom-logo',
		array(
			'height'      => 96,
			'width'       => 96,
			'flex-height' => true,
			'flex-width'  => true,
		)
	);
	add_theme_support(
		'html5',
		array(
			'search-form',
			'comment-form',
			'comment-list',
			'gallery',
			'caption',
			'style',
			'script',
		)
	);

	register_nav_menus(
		array(
			'primary' => __( 'Hauptmenü', 'kgv-classic' ),
			'footer'  => __( 'Footer Menü', 'kgv-classic' ),
		)
	);
}
add_action( 'after_setup_theme', 'kgv_classic_setup' );

function kgv_classic_widgets_init() {
	register_sidebar(
		array(
			'name'          => __( 'Sidebar', 'kgv-classic' ),
			'id'            => 'sidebar-1',
			'description'   => __( 'Widget-Bereich für Sidebar und Plugin-Widgets.', 'kgv-classic' ),
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h3 class="widget-title">',
			'after_title'   => '</h3>',
		)
	);
}
add_action( 'widgets_init', 'kgv_classic_widgets_init' );

function kgv_classic_enqueue_assets() {
	wp_enqueue_style(
		'kgv-classic-style',
		get_stylesheet_uri(),
		array(),
		wp_get_theme()->get( 'Version' )
	);

	wp_enqueue_script(
		'kgv-classic-slider',
		get_template_directory_uri() . '/assets/js/slider.js',
		array(),
		wp_get_theme()->get( 'Version' ),
		true
	);
}
add_action( 'wp_enqueue_scripts', 'kgv_classic_enqueue_assets' );

function kgv_sanitize_checkbox( $checked ) {
	return ( isset( $checked ) && true == $checked ) ? 1 : 0;
}

function kgv_sanitize_login_redirect_target( $value ) {
	$allowed = array( 'dashboard', 'home', 'default', 'custom' );

	if ( in_array( $value, $allowed, true ) ) {
		return $value;
	}

	return 'dashboard';
}

function kgv_get_font_choices() {
	return array(
		'Arial, Helvetica, sans-serif' => 'Arial',
		'"Trebuchet MS", Helvetica, sans-serif' => 'Trebuchet MS',
		'Georgia, "Times New Roman", serif' => 'Georgia',
		'"Times New Roman", Times, serif' => 'Times New Roman',
		'Verdana, Geneva, sans-serif' => 'Verdana',
		'Tahoma, Geneva, sans-serif' => 'Tahoma',
		'"Courier New", Courier, monospace' => 'Courier New',
		'system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif' => 'System UI',
	);
}

function kgv_sanitize_font_choice( $value ) {
	$choices = kgv_get_font_choices();

	if ( array_key_exists( $value, $choices ) ) {
		return $value;
	}

	return 'Arial, Helvetica, sans-serif';
}

function kgv_sanitize_range_0_20( $value ) {
	$value = absint( $value );

	if ( $value < 0 ) {
		$value = 0;
	}

	if ( $value > 20 ) {
		$value = 20;
	}

	return $value;
}

function kgv_sanitize_range_0_80( $value ) {
	$value = absint( $value );

	if ( $value < 0 ) {
		$value = 0;
	}

	if ( $value > 80 ) {
		$value = 80;
	}

	return $value;
}

function kgv_sanitize_range_0_120( $value ) {
	$value = absint( $value );

	if ( $value < 0 ) {
		$value = 0;
	}

	if ( $value > 120 ) {
		$value = 120;
	}

	return $value;
}

function kgv_customize_slider( $wp_customize ) {

	$wp_customize->add_section(
		'kgv_slider_section',
		array(
			'title'    => 'Slider',
			'priority' => 30,
		)
	);

	for ( $i = 1; $i <= 3; $i++ ) {

		$wp_customize->add_setting( "kgv_slide_{$i}_image" );
		$wp_customize->add_control(
			new WP_Customize_Image_Control(
				$wp_customize,
				"kgv_slide_{$i}_image",
				array(
					'label'   => "Slide {$i} Bild",
					'section' => 'kgv_slider_section',
				)
			)
		);

		$wp_customize->add_setting(
			"kgv_slide_{$i}_title",
			array(
				'sanitize_callback' => 'sanitize_text_field',
			)
		);
		$wp_customize->add_control(
			"kgv_slide_{$i}_title",
			array(
				'label'   => "Slide {$i} Titel",
				'section' => 'kgv_slider_section',
				'type'    => 'text',
			)
		);

		$wp_customize->add_setting(
			"kgv_slide_{$i}_text",
			array(
				'sanitize_callback' => 'sanitize_textarea_field',
			)
		);
		$wp_customize->add_control(
			"kgv_slide_{$i}_text",
			array(
				'label'   => "Slide {$i} Text",
				'section' => 'kgv_slider_section',
				'type'    => 'textarea',
			)
		);

		$wp_customize->add_setting(
			"kgv_slide_{$i}_show_button",
			array(
				'sanitize_callback' => 'kgv_sanitize_checkbox',
			)
		);
		$wp_customize->add_control(
			"kgv_slide_{$i}_show_button",
			array(
				'label'   => 'Button anzeigen',
				'section' => 'kgv_slider_section',
				'type'    => 'checkbox',
			)
		);

		$wp_customize->add_setting(
			"kgv_slide_{$i}_button",
			array(
				'sanitize_callback' => 'esc_url_raw',
			)
		);
		$wp_customize->add_control(
			"kgv_slide_{$i}_button",
			array(
				'label'   => 'Button Link',
				'section' => 'kgv_slider_section',
				'type'    => 'url',
			)
		);

		$wp_customize->add_setting(
			"kgv_slide_{$i}_label",
			array(
				'sanitize_callback' => 'sanitize_text_field',
			)
		);
		$wp_customize->add_control(
			"kgv_slide_{$i}_label",
			array(
				'label'   => 'Button Text',
				'section' => 'kgv_slider_section',
				'type'    => 'text',
			)
		);
	}
}
add_action( 'customize_register', 'kgv_customize_slider' );

function kgv_classic_slider_items() {
	$slides = array();

	for ( $i = 1; $i <= 3; $i++ ) {
		$image = get_theme_mod( "kgv_slide_{$i}_image" );

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

		$slides[] = array(
			'image'       => $image,
			'title'       => get_theme_mod( "kgv_slide_{$i}_title" ),
			'text'        => get_theme_mod( "kgv_slide_{$i}_text" ),
			'button'      => get_theme_mod( "kgv_slide_{$i}_button" ),
			'label'       => get_theme_mod( "kgv_slide_{$i}_label" ),
			'show_button' => get_theme_mod( "kgv_slide_{$i}_show_button" ),
		);
	}

	return $slides;
}

function kgv_exclude_featured_post_from_home( $query ) {
	if ( is_admin() || ! $query->is_main_query() ) {
		return;
	}

	if ( ! $query->is_home() && ! $query->is_front_page() ) {
		return;
	}

	$featured_post_id = absint( get_theme_mod( 'kgv_featured_post_id' ) );

	if ( ! $featured_post_id ) {
		return;
	}

	$post__not_in = $query->get( 'post__not_in' );
	if ( ! is_array( $post__not_in ) ) {
		$post__not_in = array();
	}

	$post__not_in[] = $featured_post_id;

	$query->set( 'post__not_in', array_unique( array_map( 'absint', $post__not_in ) ) );
}
add_action( 'pre_get_posts', 'kgv_exclude_featured_post_from_home', 20 );

function kgv_featured_post_first( $query ) {
	if ( is_admin() || ! $query->is_main_query() ) {
		return;
	}

	if ( ! $query->is_home() && ! $query->is_front_page() ) {
		return;
	}

	$featured_post_id = absint( get_theme_mod( 'kgv_featured_post_id' ) );

	if ( ! $featured_post_id ) {
		return;
	}

	$post__in = $query->get( 'post__in' );
	if ( ! is_array( $post__in ) ) {
		$post__in = array();
	}

	array_unshift( $post__in, $featured_post_id );
	$post__in = array_unique( array_map( 'absint', $post__in ) );

	$query->set( 'post__in', $post__in );
	$query->set( 'orderby', 'post__in' );
}
add_action( 'pre_get_posts', 'kgv_featured_post_first' );

function kgv_customize_register_featured_post( $wp_customize ) {
	$wp_customize->add_section(
		'kgv_home_settings',
		array(
			'title'    => 'Startseite',
			'priority' => 30,
		)
	);

	$wp_customize->add_setting(
		'kgv_featured_post_id',
		array(
			'default'           => '',
			'sanitize_callback' => 'absint',
		)
	);

	$wp_customize->add_control(
		'kgv_featured_post_id',
		array(
			'label'   => 'Beitrags-ID oben anzeigen',
			'section' => 'kgv_home_settings',
			'type'    => 'number',
		)
	);
}
add_action( 'customize_register', 'kgv_customize_register_featured_post' );

function kgv_customize_login_settings( $wp_customize ) {
	$wp_customize->add_section(
		'kgv_login_settings',
		array(
			'title'    => 'Login',
			'priority' => 34,
		)
	);

	$wp_customize->add_setting(
		'kgv_login_redirect_target',
		array(
			'default'           => 'dashboard',
			'sanitize_callback' => 'kgv_sanitize_login_redirect_target',
		)
	);

	$wp_customize->add_control(
		'kgv_login_redirect_target',
		array(
			'label'   => 'Weiterleitung nach erfolgreichem Login',
			'section' => 'kgv_login_settings',
			'type'    => 'select',
			'choices' => array(
				'dashboard' => 'Dashboard',
				'home'      => 'Startseite',
				'default'   => 'WordPress-Standard',
				'custom'    => 'Benutzerdefinierte URL',
			),
		)
	);

	$wp_customize->add_setting(
		'kgv_show_auth_menu_link',
		array(
			'default'           => 1,
			'sanitize_callback' => 'kgv_sanitize_checkbox',
		)
	);

	$wp_customize->add_control(
		'kgv_show_auth_menu_link',
		array(
			'label'   => 'Login-/Logout-Link im Hauptmenü anzeigen',
			'section' => 'kgv_login_settings',
			'type'    => 'checkbox',
		)
	);

	$wp_customize->add_setting(
		'kgv_login_redirect_custom_url',
		array(
			'default'           => '',
			'sanitize_callback' => 'esc_url_raw',
		)
	);

	$wp_customize->add_control(
		'kgv_login_redirect_custom_url',
		array(
			'label'       => 'Eigene Weiterleitungs-URL',
			'description' => 'Wird nur verwendet, wenn oben „Benutzerdefinierte URL“ gewählt ist.',
			'section'     => 'kgv_login_settings',
			'type'        => 'url',
		)
	);
}
add_action( 'customize_register', 'kgv_customize_login_settings' );

function kgv_customize_design_settings( $wp_customize ) {

	$wp_customize->add_section(
		'kgv_design_settings',
		array(
			'title'    => 'Design',
			'priority' => 35,
		)
	);

	$wp_customize->add_setting(
		'kgv_color_bg',
		array(
			'default'           => '#f6f1e8',
			'sanitize_callback' => 'sanitize_hex_color',
		)
	);
	$wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'kgv_color_bg',
			array(
				'label'   => 'Hintergrundfarbe',
				'section' => 'kgv_design_settings',
			)
		)
	);

	$wp_customize->add_setting(
		'kgv_color_card',
		array(
			'default'           => '#ffffff',
			'sanitize_callback' => 'sanitize_hex_color',
		)
	);
	$wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'kgv_color_card',
			array(
				'label'   => 'Kartenfarbe',
				'section' => 'kgv_design_settings',
			)
		)
	);

	$wp_customize->add_setting(
		'kgv_color_text',
		array(
			'default'           => '#1d1d1d',
			'sanitize_callback' => 'sanitize_hex_color',
		)
	);
	$wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'kgv_color_text',
			array(
				'label'   => 'Textfarbe',
				'section' => 'kgv_design_settings',
			)
		)
	);

	$wp_customize->add_setting(
		'kgv_color_muted',
		array(
			'default'           => '#6c6c6c',
			'sanitize_callback' => 'sanitize_hex_color',
		)
	);
	$wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'kgv_color_muted',
			array(
				'label'   => 'Sekundärtext',
				'section' => 'kgv_design_settings',
			)
		)
	);

	$wp_customize->add_setting(
		'kgv_color_accent',
		array(
			'default'           => '#245c4f',
			'sanitize_callback' => 'sanitize_hex_color',
		)
	);
	$wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'kgv_color_accent',
			array(
				'label'   => 'Akzentfarbe',
				'section' => 'kgv_design_settings',
			)
		)
	);

	$wp_customize->add_setting(
		'kgv_color_border',
		array(
			'default'           => '#e4dacb',
			'sanitize_callback' => 'sanitize_hex_color',
		)
	);
	$wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'kgv_color_border',
			array(
				'label'   => 'Rahmenfarbe',
				'section' => 'kgv_design_settings',
			)
		)
	);

	$wp_customize->add_setting(
		'kgv_color_header_bg',
		array(
			'default'           => '#ffffff',
			'sanitize_callback' => 'sanitize_hex_color',
		)
	);
	$wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'kgv_color_header_bg',
			array(
				'label'   => 'Header Hintergrund',
				'section' => 'kgv_design_settings',
			)
		)
	);

	$wp_customize->add_setting(
		'kgv_color_footer_bg',
		array(
			'default'           => '#ffffff',
			'sanitize_callback' => 'sanitize_hex_color',
		)
	);
	$wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'kgv_color_footer_bg',
			array(
				'label'   => 'Footer Hintergrund',
				'section' => 'kgv_design_settings',
			)
		)
	);

	$wp_customize->add_setting(
		'kgv_site_title_color',
		array(
			'default'           => '#1d1d1d',
			'sanitize_callback' => 'sanitize_hex_color',
		)
	);
	$wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'kgv_site_title_color',
			array(
				'label'   => 'Website-Titel Farbe',
				'section' => 'kgv_design_settings',
			)
		)
	);

	$wp_customize->add_setting(
		'kgv_site_title_size',
		array(
			'default'           => 29,
			'sanitize_callback' => 'kgv_sanitize_range_0_120',
		)
	);
	$wp_customize->add_control(
		'kgv_site_title_size',
		array(
			'label'       => 'Website-Titel Schriftgröße in px',
			'section'     => 'kgv_design_settings',
			'type'        => 'number',
			'input_attrs' => array(
				'min'  => 10,
				'max'  => 120,
				'step' => 1,
			),
		)
	);

	$wp_customize->add_setting(
		'kgv_site_description_color',
		array(
			'default'           => '#6c6c6c',
			'sanitize_callback' => 'sanitize_hex_color',
		)
	);
	$wp_customize->add_control(
		new WP_Customize_Color_Control(
			$wp_customize,
			'kgv_site_description_color',
			array(
				'label'   => 'Untertitel Farbe',
				'section' => 'kgv_design_settings',
			)
		)
	);

	$wp_customize->add_setting(
		'kgv_site_description_size',
		array(
			'default'           => 15,
			'sanitize_callback' => 'kgv_sanitize_range_0_120',
		)
	);
	$wp_customize->add_control(
		'kgv_site_description_size',
		array(
			'label'       => 'Untertitel Schriftgröße in px',
			'section'     => 'kgv_design_settings',
			'type'        => 'number',
			'input_attrs' => array(
				'min'  => 10,
				'max'  => 120,
				'step' => 1,
			),
		)
	);

	$wp_customize->add_setting(
		'kgv_body_font',
		array(
			'default'           => 'Arial, Helvetica, sans-serif',
			'sanitize_callback' => 'kgv_sanitize_font_choice',
		)
	);
	$wp_customize->add_control(
		'kgv_body_font',
		array(
			'label'   => 'Schriftart Fließtext',
			'section' => 'kgv_design_settings',
			'type'    => 'select',
			'choices' => kgv_get_font_choices(),
		)
	);

	$wp_customize->add_setting(
		'kgv_heading_font',
		array(
			'default'           => 'Arial, Helvetica, sans-serif',
			'sanitize_callback' => 'kgv_sanitize_font_choice',
		)
	);
	$wp_customize->add_control(
		'kgv_heading_font',
		array(
			'label'   => 'Schriftart Überschriften',
			'section' => 'kgv_design_settings',
			'type'    => 'select',
			'choices' => kgv_get_font_choices(),
		)
	);

	$wp_customize->add_setting(
		'kgv_border_width',
		array(
			'default'           => 1,
			'sanitize_callback' => 'kgv_sanitize_range_0_20',
		)
	);
	$wp_customize->add_control(
		'kgv_border_width',
		array(
			'label'       => 'Rahmenstärke außen in px',
			'section'     => 'kgv_design_settings',
			'type'        => 'number',
			'input_attrs' => array(
				'min'  => 0,
				'max'  => 20,
				'step' => 1,
			),
		)
	);

	$wp_customize->add_setting(
		'kgv_inner_border_width',
		array(
			'default'           => 1,
			'sanitize_callback' => 'kgv_sanitize_range_0_20',
		)
	);
	$wp_customize->add_control(
		'kgv_inner_border_width',
		array(
			'label'       => 'Rahmenstärke innen in px',
			'section'     => 'kgv_design_settings',
			'type'        => 'number',
			'input_attrs' => array(
				'min'  => 0,
				'max'  => 20,
				'step' => 1,
			),
		)
	);

	$wp_customize->add_setting(
		'kgv_show_borders',
		array(
			'default'           => 1,
			'sanitize_callback' => 'kgv_sanitize_checkbox',
		)
	);
	$wp_customize->add_control(
		'kgv_show_borders',
		array(
			'label'   => 'Außenrahmen anzeigen',
			'section' => 'kgv_design_settings',
			'type'    => 'checkbox',
		)
	);

	$wp_customize->add_setting(
		'kgv_show_inner_borders',
		array(
			'default'           => 1,
			'sanitize_callback' => 'kgv_sanitize_checkbox',
		)
	);
	$wp_customize->add_control(
		'kgv_show_inner_borders',
		array(
			'label'   => 'Innenrahmen anzeigen',
			'section' => 'kgv_design_settings',
			'type'    => 'checkbox',
		)
	);

	$wp_customize->add_setting(
		'kgv_card_padding',
		array(
			'default'           => 18,
			'sanitize_callback' => 'kgv_sanitize_range_0_120',
		)
	);
	$wp_customize->add_control(
		'kgv_card_padding',
		array(
			'label'       => 'Innenabstand Karten in px',
			'section'     => 'kgv_design_settings',
			'type'        => 'number',
			'input_attrs' => array(
				'min'  => 0,
				'max'  => 120,
				'step' => 1,
			),
		)
	);

	$wp_customize->add_setting(
		'kgv_widget_padding',
		array(
			'default'           => 16,
			'sanitize_callback' => 'kgv_sanitize_range_0_120',
		)
	);
	$wp_customize->add_control(
		'kgv_widget_padding',
		array(
			'label'       => 'Innenabstand Widgets in px',
			'section'     => 'kgv_design_settings',
			'type'        => 'number',
			'input_attrs' => array(
				'min'  => 0,
				'max'  => 120,
				'step' => 1,
			),
		)
	);

	$wp_customize->add_setting(
		'kgv_inner_box_padding',
		array(
			'default'           => 14,
			'sanitize_callback' => 'kgv_sanitize_range_0_120',
		)
	);
	$wp_customize->add_control(
		'kgv_inner_box_padding',
		array(
			'label'       => 'Innenabstand Innenboxen in px',
			'section'     => 'kgv_design_settings',
			'type'        => 'number',
			'input_attrs' => array(
				'min'  => 0,
				'max'  => 120,
				'step' => 1,
			),
		)
	);

	$wp_customize->add_setting(
		'kgv_section_gap',
		array(
			'default'           => 22,
			'sanitize_callback' => 'kgv_sanitize_range_0_120',
		)
	);
	$wp_customize->add_control(
		'kgv_section_gap',
		array(
			'label'       => 'Abstand Content/Sidebar in px',
			'section'     => 'kgv_design_settings',
			'type'        => 'number',
			'input_attrs' => array(
				'min'  => 0,
				'max'  => 120,
				'step' => 1,
			),
		)
	);

	$wp_customize->add_setting(
		'kgv_card_margin_bottom',
		array(
			'default'           => 20,
			'sanitize_callback' => 'kgv_sanitize_range_0_120',
		)
	);
	$wp_customize->add_control(
		'kgv_card_margin_bottom',
		array(
			'label'       => 'Abstand unter Karten in px',
			'section'     => 'kgv_design_settings',
			'type'        => 'number',
			'input_attrs' => array(
				'min'  => 0,
				'max'  => 120,
				'step' => 1,
			),
		)
	);

	$wp_customize->add_setting(
		'kgv_widget_margin_bottom',
		array(
			'default'           => 24,
			'sanitize_callback' => 'kgv_sanitize_range_0_120',
		)
	);
	$wp_customize->add_control(
		'kgv_widget_margin_bottom',
		array(
			'label'       => 'Abstand unter Widgets in px',
			'section'     => 'kgv_design_settings',
			'type'        => 'number',
			'input_attrs' => array(
				'min'  => 0,
				'max'  => 120,
				'step' => 1,
			),
		)
	);

	$wp_customize->add_setting(
		'kgv_main_padding_top',
		array(
			'default'           => 28,
			'sanitize_callback' => 'kgv_sanitize_range_0_120',
		)
	);
	$wp_customize->add_control(
		'kgv_main_padding_top',
		array(
			'label'       => 'Seitenabstand oben in px',
			'section'     => 'kgv_design_settings',
			'type'        => 'number',
			'input_attrs' => array(
				'min'  => 0,
				'max'  => 120,
				'step' => 1,
			),
		)
	);

	$wp_customize->add_setting(
		'kgv_main_padding_bottom',
		array(
			'default'           => 36,
			'sanitize_callback' => 'kgv_sanitize_range_0_120',
		)
	);
	$wp_customize->add_control(
		'kgv_main_padding_bottom',
		array(
			'label'       => 'Seitenabstand unten in px',
			'section'     => 'kgv_design_settings',
			'type'        => 'number',
			'input_attrs' => array(
				'min'  => 0,
				'max'  => 120,
				'step' => 1,
			),
		)
	);
}
add_action( 'customize_register', 'kgv_customize_design_settings' );

function kgv_customizer_dynamic_css() {
	$bg                  = get_theme_mod( 'kgv_color_bg', '#f6f1e8' );
	$card                = get_theme_mod( 'kgv_color_card', '#ffffff' );
	$text                = get_theme_mod( 'kgv_color_text', '#1d1d1d' );
	$muted               = get_theme_mod( 'kgv_color_muted', '#6c6c6c' );
	$accent              = get_theme_mod( 'kgv_color_accent', '#245c4f' );
	$border              = get_theme_mod( 'kgv_color_border', '#e4dacb' );
	$header_bg           = get_theme_mod( 'kgv_color_header_bg', '#ffffff' );
	$footer_bg           = get_theme_mod( 'kgv_color_footer_bg', '#ffffff' );
	$site_title_color    = get_theme_mod( 'kgv_site_title_color', '#1d1d1d' );
	$site_title_size     = absint( get_theme_mod( 'kgv_site_title_size', 29 ) );
	$site_description_color = get_theme_mod( 'kgv_site_description_color', '#6c6c6c' );
	$site_description_size  = absint( get_theme_mod( 'kgv_site_description_size', 15 ) );
	$body_font           = get_theme_mod( 'kgv_body_font', 'Arial, Helvetica, sans-serif' );
	$heading_font        = get_theme_mod( 'kgv_heading_font', 'Arial, Helvetica, sans-serif' );
	$border_width        = absint( get_theme_mod( 'kgv_border_width', 1 ) );
	$inner_border_width  = absint( get_theme_mod( 'kgv_inner_border_width', 1 ) );
	$show_borders        = (int) get_theme_mod( 'kgv_show_borders', 1 );
	$show_inner_borders  = (int) get_theme_mod( 'kgv_show_inner_borders', 1 );
	$card_padding        = absint( get_theme_mod( 'kgv_card_padding', 18 ) );
	$widget_padding      = absint( get_theme_mod( 'kgv_widget_padding', 16 ) );
	$inner_box_padding   = absint( get_theme_mod( 'kgv_inner_box_padding', 14 ) );
	$section_gap         = absint( get_theme_mod( 'kgv_section_gap', 22 ) );
	$card_margin_bottom  = absint( get_theme_mod( 'kgv_card_margin_bottom', 20 ) );
	$widget_margin_bottom = absint( get_theme_mod( 'kgv_widget_margin_bottom', 24 ) );
	$main_padding_top    = absint( get_theme_mod( 'kgv_main_padding_top', 28 ) );
	$main_padding_bottom = absint( get_theme_mod( 'kgv_main_padding_bottom', 36 ) );

	$outer_border_value = $show_borders ? $border_width : 0;
	$inner_border_value = $show_inner_borders ? $inner_border_width : 0;

	$css = "
	:root{
		--kgv-bg: {$bg};
		--kgv-card: {$card};
		--kgv-text: {$text};
		--kgv-muted: {$muted};
		--kgv-accent: {$accent};
		--kgv-border: {$border};
		--kgv-header-bg: {$header_bg};
		--kgv-footer-bg: {$footer_bg};
		--kgv-site-title-color: {$site_title_color};
		--kgv-site-title-size: {$site_title_size}px;
		--kgv-site-description-color: {$site_description_color};
		--kgv-site-description-size: {$site_description_size}px;
		--kgv-body-font: {$body_font};
		--kgv-heading-font: {$heading_font};
		--kgv-border-width: {$outer_border_value}px;
		--kgv-inner-border-width: {$inner_border_value}px;
		--kgv-card-padding: {$card_padding}px;
		--kgv-widget-padding: {$widget_padding}px;
		--kgv-inner-box-padding: {$inner_box_padding}px;
		--kgv-section-gap: {$section_gap}px;
		--kgv-card-margin-bottom: {$card_margin_bottom}px;
		--kgv-widget-margin-bottom: {$widget_margin_bottom}px;
		--kgv-main-padding-top: {$main_padding_top}px;
		--kgv-main-padding-bottom: {$main_padding_bottom}px;
	}

	.site-title{
		color: var(--kgv-site-title-color);
		font-size: var(--kgv-site-title-size);
	}

	.site-description{
		color: var(--kgv-site-description-color);
		font-size: var(--kgv-site-description-size);
	}

	.site-main{
		padding: var(--kgv-main-padding-top) 0 var(--kgv-main-padding-bottom);
	}

	.content-grid{
		gap: var(--kgv-section-gap);
	}

	.post-card,
	.page-content-card{
		margin: 0 0 var(--kgv-card-margin-bottom);
	}

	.card-body,
	.page-content-card,
	.post-card.single-post,
	.post-card .single-post{
		padding: var(--kgv-card-padding);
	}

	.archive-header,
	.comments-area{
		padding: var(--kgv-card-padding);
	}

	.sidebar-area .widget{
		padding: var(--kgv-widget-padding);
		margin: 0 0 var(--kgv-widget-margin-bottom);
	}

	.comment-body,
	.kgv-termin-meta-item,
	.kgv-termin-box{
		padding: var(--kgv-inner-box-padding);
	}

	.widget_recent_entries ul li{
		margin-bottom: calc(var(--kgv-widget-padding) - 2px);
		padding-bottom: calc(var(--kgv-widget-padding) - 6px);
	}

	@media (max-width: 900px){
		.sidebar-area .widget{
			margin-bottom: var(--kgv-card-margin-bottom);
		}
	}
	";

	wp_add_inline_style( 'kgv-classic-style', $css );
}
add_action( 'wp_enqueue_scripts', 'kgv_customizer_dynamic_css', 20 );

function kgv_sanitize_page_checkbox_list( $value ) {
	if ( is_array( $value ) ) {
		$ids = array_map( 'absint', $value );
	} else {
		$ids = array_map( 'absint', explode( ',', (string) $value ) );
	}

	$ids = array_filter( $ids );
	$ids = array_unique( $ids );

	return implode( ',', $ids );
}

function kgv_get_hidden_page_date_ids() {
	$raw = get_theme_mod( 'kgv_hide_page_date_ids', '' );

	if ( empty( $raw ) ) {
		return array();
	}

	return array_unique( array_filter( array_map( 'absint', explode( ',', (string) $raw ) ) ) );
}

function kgv_customize_page_meta_settings( $wp_customize ) {

	if ( class_exists( 'WP_Customize_Control' ) && ! class_exists( 'KGV_Customize_Page_Checkbox_List_Control' ) ) {
		class KGV_Customize_Page_Checkbox_List_Control extends WP_Customize_Control {
			public $type = 'kgv-page-checkbox-list';

			public function render_content() {
				$pages = get_pages();

				if ( empty( $pages ) ) {
					echo '<span class="customize-control-title">' . esc_html( $this->label ) . '</span>';
					echo '<p>Keine Seiten gefunden.</p>';
					return;
				}

				$values = $this->value();
				if ( ! is_array( $values ) ) {
					$values = array_filter( array_map( 'absint', explode( ',', (string) $values ) ) );
				}

				echo '<span class="customize-control-title">' . esc_html( $this->label ) . '</span>';

				if ( ! empty( $this->description ) ) {
					echo '<span class="description customize-control-description">' . esc_html( $this->description ) . '</span>';
				}

				echo '<div style="max-height:220px;overflow:auto;border:1px solid #ddd;padding:10px;background:#fff;">';

				foreach ( $pages as $page ) {
					$checked = in_array( (int) $page->ID, $values, true ) ? 'checked="checked"' : '';

					echo '<label style="display:block;margin-bottom:8px;">';
					echo '<input type="checkbox" value="' . esc_attr( $page->ID ) . '" ' . $checked . ' onchange="
						var values = [];
						this.closest(\'div\').querySelectorAll(\'input[type=checkbox]:checked\').forEach(function(el){
							values.push(el.value);
						});
						wp.customize.instance(\'' . esc_js( $this->id ) . '\').set(values.join(\',\'));
					"> ';
					echo esc_html( $page->post_title ? $page->post_title : '(Ohne Titel)' );
					echo ' <small style=\"color:#666;\">[ID ' . esc_html( $page->ID ) . ']</small>';
					echo '</label>';
				}

				echo '</div>';
			}
		}
	}

	$wp_customize->add_section( 'kgv_page_meta_settings', array(
		'title'    => 'Seiten',
		'priority' => 35,
	) );

	$wp_customize->add_setting( 'kgv_show_page_date', array(
		'default'           => 1,
		'sanitize_callback' => 'absint',
	) );

	$wp_customize->add_control( 'kgv_show_page_date', array(
		'label'   => 'Datum auf Seiten grundsätzlich anzeigen',
		'section' => 'kgv_page_meta_settings',
		'type'    => 'checkbox',
	) );

	$wp_customize->add_setting( 'kgv_hide_page_date_ids', array(
		'default'           => '',
		'sanitize_callback' => 'kgv_sanitize_page_checkbox_list',
	) );

	$wp_customize->add_control(
		new KGV_Customize_Page_Checkbox_List_Control(
			$wp_customize,
			'kgv_hide_page_date_ids',
			array(
				'label'       => 'Datum auf ausgewählten Seiten ausblenden',
				'description' => 'Hier nur Seiten auswählen. Beiträge tauchen hier nicht auf.',
				'section'     => 'kgv_page_meta_settings',
			)
		)
	);
}
add_action( 'customize_register', 'kgv_customize_page_meta_settings' );

