<?php
/**
 * Rifeng Weifang Theme Functions
 *
 * @package Rifeng_Weifang
 * @version 1.0.0
 */

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

define( 'RF_THEME_VERSION', '1.0.0' );
define( 'RF_THEME_DIR', get_template_directory() );
define( 'RF_THEME_URI', get_template_directory_uri() );

/**
 * Theme Setup
 */
function rf_theme_setup() {
    // Add theme support
    add_theme_support( 'title-tag' );
    add_theme_support( 'post-thumbnails' );
    add_theme_support( 'custom-logo', array(
        'height'      => 100,
        'width'       => 300,
        'flex-height' => true,
        'flex-width'  => true,
    ) );
    add_theme_support( 'html5', array(
        'search-form',
        'comment-form',
        'comment-list',
        'gallery',
        'caption',
    ) );
    add_theme_support( 'responsive-embeds' );
    add_theme_support( 'align-wide' );

    // Register navigation menus
    register_nav_menus( array(
        'primary' => __( '主导航菜单', 'rifeng-weifang' ),
        'footer'  => __( '底部菜单', 'rifeng-weifang' ),
    ) );

    // Set content width
    if ( ! isset( $content_width ) ) {
        $content_width = 1200;
    }
}
add_action( 'after_setup_theme', 'rf_theme_setup' );

/**
 * Enqueue scripts and styles
 */
function rf_theme_scripts() {
    // Main stylesheet
    wp_enqueue_style( 'rf-theme-style', get_stylesheet_uri(), array(), RF_THEME_VERSION );
    
    // Theme CSS
    wp_enqueue_style( 'rf-theme-main', RF_THEME_URI . '/css/main.css', array(), RF_THEME_VERSION );
    
    // Theme JS
    wp_enqueue_script( 'rf-theme-main', RF_THEME_URI . '/js/main.js', array('jquery'), RF_THEME_VERSION, true );
    
    // Comment reply script
    if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
        wp_enqueue_script( 'comment-reply' );
    }
}
add_action( 'wp_enqueue_scripts', 'rf_theme_scripts' );

/**
 * Register Widget Areas
 */
function rf_widgets_init() {
    register_sidebar( array(
        'name'          => __( '侧边栏', 'rifeng-weifang' ),
        'id'            => 'sidebar-1',
        'description'   => __( '添加到侧边栏的挂件', 'rifeng-weifang' ),
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
        'after_widget'  => '</div>',
        'before_title'  => '<h3 class="widget-title">',
        'after_title'   => '</h3>',
    ) );
    
    register_sidebar( array(
        'name'          => __( '底部区域', 'rifeng-weifang' ),
        'id'            => 'footer-1',
        'description'   => __( '添加到页脚的挂件', 'rifeng-weifang' ),
        'before_widget' => '<div id="%1$s" class="footer-widget %2$s">',
        'after_widget'  => '</div>',
        'before_title'  => '<h4 class="widget-title">',
        'after_title'   => '</h4>',
    ) );
}
add_action( 'widgets_init', 'rf_widgets_init' );

/**
 * Custom excerpt length
 */
function rf_excerpt_length( $length ) {
    return 30;
}
add_filter( 'excerpt_length', 'rf_excerpt_length' );

/**
 * Custom excerpt more
 */
function rf_excerpt_more( $more ) {
    return '...';
}
add_filter( 'excerpt_more', 'rf_excerpt_more' );

/**
 * Add body classes
 */
function rf_body_classes( $classes ) {
    if ( is_front_page() ) {
        $classes[] = 'home-page';
    }
    return $classes;
}
add_filter( 'body_class', 'rf_body_classes' );

/**
 * Custom walker for navigation (simplified)
 */
class RF_Walker_Nav_Menu extends Walker_Nav_Menu {
    function start_lvl( &$output, $depth = 0, $args = null ) {
        $indent = str_repeat( "\t", $depth );
        $output .= "\n$indent<ul class=\"sub-menu\">\n";
    }
}

/**
 * Default menu fallback
 */
function rf_default_menu() {
    echo '<ul>';
    echo '<li><a href="' . esc_url( home_url( '/' ) ) . '">首页</a></li>';
    echo '<li><a href="' . esc_url( home_url( '/chanpin' ) ) . '">日丰产品</a></li>';
    echo '<li><a href="' . esc_url( home_url( '/anquanweishi' ) ) . '">安全卫士服务</a></li>';
    echo '<li><a href="' . esc_url( home_url( '/gongchenganli' ) ) . '">工程项目案例</a></li>';
    echo '<li><a href="' . esc_url( home_url( '/news' ) ) . '">新闻动态</a></li>';
    echo '<li><a href="' . esc_url( home_url( '/about' ) ) . '">关于我们</a></li>';
    echo '<li><a href="' . esc_url( home_url( '/lianxi' ) ) . '">联系我们</a></li>';
    echo '</ul>';
}

/**
 * Disable emoji
 */
function rf_disable_emojis() {
    remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
    remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
    remove_action( 'wp_print_styles', 'print_emoji_styles' );
    remove_action( 'admin_print_styles', 'print_emoji_styles' );
}
add_action( 'init', 'rf_disable_emojis' );

/**
 * Remove unnecessary head tags
 */
function rf_cleanup_head() {
    remove_action( 'wp_head', 'rsd_link' );
    remove_action( 'wp_head', 'wlwmanifest_link' );
    remove_action( 'wp_head', 'wp_generator' );
    remove_action( 'wp_head', 'wp_shortlink_wp_head' );
}
add_action( 'init', 'rf_cleanup_head' );

/**
 * Add custom image sizes
 */
function rf_custom_image_sizes() {
    add_image_size( 'product-thumb', 400, 300, true );
    add_image_size( 'news-thumb', 600, 400, true );
    add_image_size( 'banner-full', 1920, 600, true );
    add_image_size( 'solution-thumb', 500, 350, true );
}
add_action( 'after_setup_theme', 'rf_custom_image_sizes' );

/**
 * Get featured posts for homepage
 */
function rf_get_featured_posts( $count = 3 ) {
    $args = array(
        'posts_per_page'      => $count,
        'post_status'         => 'publish',
        'ignore_sticky_posts' => true,
    );
    return new WP_Query( $args );
}

/**
 * Breadcrumb function
 */
function rf_breadcrumb() {
    if ( is_front_page() ) return;
    
    echo '<div class="breadcrumb">';
    echo '<a href="' . esc_url( home_url( '/' ) ) . '">首页</a>';
    
    if ( is_single() ) {
        $categories = get_the_category();
        if ( $categories ) {
            echo ' &gt; <a href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . $categories[0]->name . '</a>';
        }
        echo ' &gt; ' . get_the_title();
    } elseif ( is_page() ) {
        echo ' &gt; ' . get_the_title();
    } elseif ( is_category() ) {
        echo ' &gt; ' . single_cat_title( '', false );
    } elseif ( is_search() ) {
        echo ' &gt; 搜索结果: ' . get_search_query();
    } elseif ( is_archive() ) {
        echo ' &gt; ' . get_the_archive_title();
    }
    
    echo '</div>';
}
