checkvorteil/functions.php
2025-06-16 15:14:17 +02:00

511 lines
18 KiB
PHP

<?php
// filepath: /home/michi/landingpages/checkvorteil-wordpress/functions.php
// Theme Setup
function checkvorteil_setup() {
// Add theme support
add_theme_support('title-tag');
add_theme_support('post-thumbnails');
add_theme_support('html5', array('search-form', 'comment-form', 'comment-list', 'gallery', 'caption'));
// Custom Logo Support mit erweiterten Optionen
add_theme_support('custom-logo', array(
'height' => 60,
'width' => 200,
'flex-width' => true,
'flex-height' => true,
'header-text' => array('site-title', 'site-description'),
'unlink-homepage-logo' => true,
));
// Register navigation menus
register_nav_menus(array(
'primary' => __('Primary Menu', 'checkvorteil'),
));
}
add_action('after_setup_theme', 'checkvorteil_setup');
// Custom Logo Function
function checkvorteil_custom_logo() {
if (has_custom_logo()) {
$logo_id = get_theme_mod('custom_logo');
$logo = wp_get_attachment_image_src($logo_id, 'full');
if ($logo) {
echo '<img src="' . esc_url($logo[0]) . '" alt="' . get_bloginfo('name') . '" class="custom-logo" style="max-height: 40px; width: auto;">';
}
} else {
// Fallback zu logo.png im Theme-Verzeichnis
$logo_path = get_template_directory_uri() . '/images/logo.png';
if (file_exists(get_template_directory() . '/images/logo.png')) {
echo '<img src="' . esc_url($logo_path) . '" alt="' . get_bloginfo('name') . '" class="default-logo" style="max-height: 40px; width: auto;">';
} else {
// Text-Fallback mit Gradient
echo '<span class="text-logo gradient-text">' . get_bloginfo('name') . '</span>';
}
}
}
// Enqueue scripts and styles
function checkvorteil_scripts() {
wp_enqueue_style('checkvorteil-style', get_stylesheet_uri(), array(), '1.0.0');
wp_enqueue_script('checkvorteil-script', get_template_directory_uri() . '/js/main.js', array(), '1.0.0', true);
}
add_action('wp_enqueue_scripts', 'checkvorteil_scripts');
// Custom Post Type: Services
function register_services_post_type() {
$labels = array(
'name' => 'Services',
'singular_name' => 'Service',
'menu_name' => 'Services',
'add_new' => 'Neuer Service',
'add_new_item' => 'Neuen Service hinzufügen',
'edit_item' => 'Service bearbeiten',
'new_item' => 'Neuer Service',
'view_item' => 'Service ansehen',
'search_items' => 'Services suchen',
'not_found' => 'Keine Services gefunden',
'not_found_in_trash' => 'Keine Services im Papierkorb gefunden'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array('slug' => 'service'),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 20,
'menu_icon' => 'dashicons-admin-tools',
'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'custom-fields')
);
register_post_type('service', $args);
}
add_action('init', 'register_services_post_type');
// Custom Post Type: KI Tools
function register_kitools_post_type() {
$labels = array(
'name' => 'KI-Tools',
'singular_name' => 'KI-Tool',
'menu_name' => 'KI-Tools',
'add_new' => 'Neues KI-Tool',
'add_new_item' => 'Neues KI-Tool hinzufügen',
'edit_item' => 'KI-Tool bearbeiten',
'new_item' => 'Neues KI-Tool',
'view_item' => 'KI-Tool ansehen',
'search_items' => 'KI-Tools suchen',
'not_found' => 'Keine KI-Tools gefunden',
'not_found_in_trash' => 'Keine KI-Tools im Papierkorb gefunden'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array('slug' => 'ki-tool'),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 21,
'menu_icon' => 'dashicons-admin-network',
'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'custom-fields')
);
register_post_type('kitool', $args);
}
add_action('init', 'register_kitools_post_type');
// Custom Taxonomy: Service Categories
function register_service_taxonomy() {
$labels = array(
'name' => 'Service Kategorien',
'singular_name' => 'Service Kategorie',
'search_items' => 'Kategorien suchen',
'all_items' => 'Alle Kategorien',
'parent_item' => 'Übergeordnete Kategorie',
'parent_item_colon' => 'Übergeordnete Kategorie:',
'edit_item' => 'Kategorie bearbeiten',
'update_item' => 'Kategorie aktualisieren',
'add_new_item' => 'Neue Kategorie hinzufügen',
'new_item_name' => 'Neuer Kategorie Name',
'menu_name' => 'Kategorien',
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'service-kategorie'),
);
register_taxonomy('service_category', array('service'), $args);
}
add_action('init', 'register_service_taxonomy');
// Custom Fields for KI-Tools
function add_kitool_meta_boxes() {
add_meta_box(
'kitool-details',
'KI-Tool Details',
'kitool_meta_box_callback',
'kitool',
'normal',
'high'
);
}
add_action('add_meta_boxes', 'add_kitool_meta_boxes');
function kitool_meta_box_callback($post) {
wp_nonce_field('kitool_meta_box', 'kitool_meta_box_nonce');
$rating = get_post_meta($post->ID, '_kitool_rating', true);
$price = get_post_meta($post->ID, '_kitool_price', true);
$features = get_post_meta($post->ID, '_kitool_features', true);
$category = get_post_meta($post->ID, '_kitool_category', true);
?>
<table class="form-table">
<tr>
<th><label for="kitool_rating">Bewertung (1-5)</label></th>
<td><input type="number" id="kitool_rating" name="kitool_rating" value="<?php echo esc_attr($rating); ?>" min="1" max="5" /></td>
</tr>
<tr>
<th><label for="kitool_price">Preis</label></th>
<td><input type="text" id="kitool_price" name="kitool_price" value="<?php echo esc_attr($price); ?>" placeholder="z.B. 20€/Monat" /></td>
</tr>
<tr>
<th><label for="kitool_category">Kategorie</label></th>
<td>
<select id="kitool_category" name="kitool_category">
<option value="">Kategorie wählen</option>
<option value="Content" <?php selected($category, 'Content'); ?>>Content Erstellung</option>
<option value="Design" <?php selected($category, 'Design'); ?>>Design</option>
<option value="Automatisierung" <?php selected($category, 'Automatisierung'); ?>>Automatisierung</option>
<option value="Analytics" <?php selected($category, 'Analytics'); ?>>Analytics</option>
</select>
</td>
</tr>
<tr>
<th><label for="kitool_features">Features (eine pro Zeile)</label></th>
<td><textarea id="kitool_features" name="kitool_features" rows="5" cols="50"><?php echo esc_textarea($features); ?></textarea></td>
</tr>
</table>
<?php
}
function save_kitool_meta_box($post_id) {
if (!isset($_POST['kitool_meta_box_nonce'])) return;
if (!wp_verify_nonce($_POST['kitool_meta_box_nonce'], 'kitool_meta_box')) return;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if (!current_user_can('edit_post', $post_id)) return;
if (isset($_POST['kitool_rating'])) {
update_post_meta($post_id, '_kitool_rating', sanitize_text_field($_POST['kitool_rating']));
}
if (isset($_POST['kitool_price'])) {
update_post_meta($post_id, '_kitool_price', sanitize_text_field($_POST['kitool_price']));
}
if (isset($_POST['kitool_features'])) {
update_post_meta($post_id, '_kitool_features', sanitize_textarea_field($_POST['kitool_features']));
}
if (isset($_POST['kitool_category'])) {
update_post_meta($post_id, '_kitool_category', sanitize_text_field($_POST['kitool_category']));
}
}
add_action('save_post', 'save_kitool_meta_box');
// Custom excerpt length
function checkvorteil_excerpt_length($length) {
return 30;
}
add_filter('excerpt_length', 'checkvorteil_excerpt_length');
// Remove default excerpt dots
function checkvorteil_excerpt_more($more) {
return '...';
}
add_filter('excerpt_more', 'checkvorteil_excerpt_more');
// Custom logo support
function checkvorteil_customize_register($wp_customize) {
// Logo-Sektion erweitern
$wp_customize->get_setting('custom_logo')->transport = 'refresh';
// Logo maximale Höhe
$wp_customize->add_setting('logo_max_height', array(
'default' => 40,
'sanitize_callback' => 'absint',
'transport' => 'refresh',
));
$wp_customize->add_control('logo_max_height', array(
'label' => 'Logo maximale Höhe (px)',
'section' => 'title_tagline',
'type' => 'number',
'description' => 'Maximale Höhe des Logos in Pixeln',
'input_attrs' => array(
'min' => 20,
'max' => 100,
'step' => 5,
),
));
$wp_customize->add_section('checkvorteil_options', array(
'title' => 'CheckVorteil Optionen',
'priority' => 30,
'description' => 'Anpassungen für Ihr CheckVorteil Theme',
));
$wp_customize->add_setting('hero_title', array(
'default' => 'CheckVorteil',
'sanitize_callback' => 'sanitize_text_field',
));
$wp_customize->add_control('hero_title', array(
'label' => 'Hero Titel',
'section' => 'checkvorteil_options',
'type' => 'text',
));
$wp_customize->add_setting('hero_subtitle', array(
'default' => 'Die besten Tools, Blog-Artikel und Services für Online-Business-Optimierung',
'sanitize_callback' => 'sanitize_textarea_field',
));
$wp_customize->add_control('hero_subtitle', array(
'label' => 'Hero Untertitel',
'section' => 'checkvorteil_options',
'type' => 'textarea',
));
// Brand Farbe
$wp_customize->add_setting('primary_color', array(
'default' => '#9333ea',
'sanitize_callback' => 'sanitize_hex_color',
'transport' => 'refresh',
));
$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'primary_color', array(
'label' => 'Primäre Akzentfarbe',
'section' => 'checkvorteil_options',
'description' => 'Hauptfarbe für Buttons und Links',
)));
}
add_action('customize_register', 'checkvorteil_customize_register');
// CSS-Variablen für Customizer ausgeben
function checkvorteil_customizer_css() {
$logo_height = get_theme_mod('logo_max_height', 40);
$primary_color = get_theme_mod('primary_color', '#9333ea');
echo '<style type="text/css">';
echo ':root {';
echo '--logo-max-height: ' . esc_attr($logo_height) . 'px;';
echo '--primary-color: ' . esc_attr($primary_color) . ';';
echo '}';
echo '.logo .custom-logo, .logo .default-logo {';
echo 'max-height: var(--logo-max-height);';
echo '}';
echo '.btn-primary {';
echo 'background-color: var(--primary-color);';
echo '}';
echo '@media (max-width: 768px) {';
echo '.logo .custom-logo, .logo .default-logo {';
echo 'max-height: calc(var(--logo-max-height) - 5px);';
echo '}';
echo '}';
echo '</style>';
}
add_action('wp_head', 'checkvorteil_customizer_css');
// Admin dashboard customization
function checkvorteil_admin_dashboard() {
echo '<div class="wrap">';
echo '<h1>CheckVorteil Dashboard</h1>';
echo '<div class="dashboard-widgets-wrap">';
echo '<div class="metabox-holder">';
// Quick stats
$post_count = wp_count_posts('post');
$service_count = wp_count_posts('service');
$kitool_count = wp_count_posts('kitool');
echo '<div class="postbox">';
echo '<h2 class="hndle">Schnelle Statistiken</h2>';
echo '<div class="inside">';
echo '<p><strong>Blog Posts:</strong> ' . $post_count->publish . '</p>';
echo '<p><strong>Services:</strong> ' . $service_count->publish . '</p>';
echo '<p><strong>KI-Tools:</strong> ' . $kitool_count->publish . '</p>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
}
function checkvorteil_admin_menu() {
add_menu_page(
'CheckVorteil Dashboard',
'CheckVorteil',
'manage_options',
'checkvorteil-dashboard',
'checkvorteil_admin_dashboard',
'dashicons-chart-area',
2
);
}
add_action('admin_menu', 'checkvorteil_admin_menu');
// Server Performance Optimierungen
function checkvorteil_performance_optimizations() {
// Remove WordPress version from head
remove_action('wp_head', 'wp_generator');
// Remove RSD link
remove_action('wp_head', 'rsd_link');
// Remove wlwmanifest.xml
remove_action('wp_head', 'wlwmanifest_link');
// Disable emoji scripts
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');
remove_action('admin_print_scripts', 'print_emoji_detection_script');
remove_action('admin_print_styles', 'print_emoji_styles');
}
add_action('init', 'checkvorteil_performance_optimizations');
// Security Headers
function checkvorteil_security_headers() {
if (!is_admin()) {
header('X-Content-Type-Options: nosniff');
header('X-Frame-Options: SAMEORIGIN');
header('X-XSS-Protection: 1; mode=block');
header('Referrer-Policy: strict-origin-when-cross-origin');
}
}
add_action('send_headers', 'checkvorteil_security_headers');
// Container-Environment Detection
function checkvorteil_is_container() {
return getenv('WORDPRESS_CONTAINER') === 'true' || file_exists('/.dockerenv');
}
// Container-optimierte URL-Konfiguration
function checkvorteil_container_setup() {
if (checkvorteil_is_container()) {
// Define URLs for container environment
if (!defined('WP_HOME')) {
define('WP_HOME', 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . $_SERVER['HTTP_HOST']);
}
if (!defined('WP_SITEURL')) {
define('WP_SITEURL', 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . $_SERVER['HTTP_HOST']);
}
}
}
add_action('init', 'checkvorteil_container_setup', 1);
// Container Health Check Endpoint
function checkvorteil_health_check() {
if (isset($_GET['health']) && $_GET['health'] === 'check') {
// Check database connection
global $wpdb;
$db_check = $wpdb->get_var("SELECT 1");
if ($db_check == 1) {
http_response_code(200);
echo json_encode(['status' => 'healthy', 'timestamp' => time()]);
} else {
http_response_code(503);
echo json_encode(['status' => 'unhealthy', 'error' => 'database']);
}
exit;
}
}
add_action('init', 'checkvorteil_health_check');
// Traefik Reverse Proxy optimierte Funktionen
function checkvorteil_traefik_setup() {
// Detect if behind Traefik reverse proxy
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) || isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
// Force HTTPS if Traefik is handling SSL termination
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
$_SERVER['HTTPS'] = 'on';
}
// Fix remote IP when behind proxy
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$forwarded_ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$_SERVER['REMOTE_ADDR'] = trim($forwarded_ips[0]);
}
// Set correct scheme for WordPress
if (!defined('FORCE_SSL_ADMIN') && isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
define('FORCE_SSL_ADMIN', true);
}
}
}
add_action('init', 'checkvorteil_traefik_setup', 0);
// Traefik Health Check Endpoint mit erweiterten Checks
function checkvorteil_traefik_health_check() {
if (isset($_GET['health']) && $_GET['health'] === 'check') {
$status = 'healthy';
$checks = [];
// Database check
global $wpdb;
try {
$db_check = $wpdb->get_var("SELECT 1");
$checks['database'] = ($db_check == 1) ? 'ok' : 'error';
} catch (Exception $e) {
$checks['database'] = 'error';
$status = 'unhealthy';
}
// File system check
$checks['filesystem'] = is_writable(WP_CONTENT_DIR) ? 'ok' : 'warning';
// Memory check
$memory_limit = wp_convert_hr_to_bytes(ini_get('memory_limit'));
$memory_usage = memory_get_usage(true);
$memory_percent = ($memory_usage / $memory_limit) * 100;
$checks['memory'] = $memory_percent < 80 ? 'ok' : 'warning';
// Theme check
$checks['theme'] = (get_template() === 'checkvorteil') ? 'ok' : 'warning';
$response = [
'status' => $status,
'timestamp' => time(),
'version' => wp_get_theme()->get('Version'),
'checks' => $checks,
'proxy_headers' => [
'x_forwarded_for' => $_SERVER['HTTP_X_FORWARDED_FOR'] ?? null,
'x_forwarded_proto' => $_SERVER['HTTP_X_FORWARDED_PROTO'] ?? null,
'x_real_ip' => $_SERVER['HTTP_X_REAL_IP'] ?? null
]
];
http_response_code($status === 'healthy' ? 200 : 503);
header('Content-Type: application/json');
echo json_encode($response, JSON_PRETTY_PRINT);
exit;
}
}
add_action('init', 'checkvorteil_traefik_health_check');
?>