<?php
/**
 * Pix主题 - JS/CSS 加载队列
 * 
 * 管理所有 JavaScript 和 CSS 文件的加载顺序
 * 
 * @package pix
 * @author Fuzzz
 * @version 1.0.4
 */
function pix_scripts() {
    global $wp_query, $post;
	wp_enqueue_style( 'pix-style', get_stylesheet_uri(), array(), PIX_VERSION );
    wp_enqueue_style( 'remixicon.css', THEME_URL . '/inc/assets/fonts/remixicon.css', array(), PIX_VERSION );
    wp_enqueue_style( 'swiper.css', THEME_URL . '/inc/assets/css/swiper-bundle.min.css', array(), PIX_VERSION );
    wp_enqueue_style( 'placeholder.min.css', THEME_URL . '/inc/assets/css/placeholder.min.css', array(), PIX_VERSION );
    wp_enqueue_style( 'popup.min.css', THEME_URL . '/inc/assets/css/popup.min.css', array(), PIX_VERSION );
    wp_enqueue_style( 'hint.css', THEME_URL . '/inc/assets/css/hint.css', array(), PIX_VERSION );
    wp_enqueue_style( 'uikit.min.css', THEME_URL . '/inc/assets/css/uikit.min.css', array(), PIX_VERSION );
    wp_enqueue_style( 'fancybox.css', THEME_URL . '/inc/assets/css/fancybox.css', array(), PIX_VERSION );
    wp_enqueue_style( 'plugin.css', THEME_URL . '/inc/assets/css/plugin.css', array(), PIX_VERSION );
    wp_enqueue_style( 'highlight.min.css', THEME_URL . '/inc/assets/css/highlight.min.css', array(), PIX_VERSION );
    wp_enqueue_style( 'user.css', THEME_URL . '/inc/assets/css/user.css', array(), PIX_VERSION );
    wp_enqueue_style( 'moment.css', THEME_URL . '/inc/assets/css/moment.css', array(), PIX_VERSION );
    wp_enqueue_style( 'pix-uploader.css', THEME_URL . '/inc/assets/css/pix-uploader.css', array(), PIX_VERSION );
    wp_enqueue_style( 'main.css', THEME_URL . '/inc/assets/css/main.css', array(), PIX_VERSION );

    // ==================== JS 按需加载 ====================

    wp_enqueue_script( 'cookies.js', THEME_URL . '/inc/assets/js/cookies.js', array('jquery'), PIX_VERSION, true );
    wp_enqueue_script( 'codeinput.js', THEME_URL . '/inc/assets/js/codeinput.js', array('jquery'), PIX_VERSION, true );
    wp_enqueue_script( 'readmore.js', THEME_URL . '/inc/assets/js/readmore.js', array('jquery'), PIX_VERSION, true );
    wp_enqueue_script( 'popup.min.js', THEME_URL . '/inc/assets/js/popup.min.js', array('jquery'), PIX_VERSION, true );
    wp_enqueue_script( 'lazyload.min.js', THEME_URL . '/inc/assets/js/lazyload.min.js', array('jquery'), PIX_VERSION, true );
    wp_enqueue_script( 'clipboard.min.js', THEME_URL . '/inc/assets/js/clipboard.min.js', array('jquery'), PIX_VERSION, true );
    wp_enqueue_script( 'toastify', THEME_URL . '/inc/assets/js/toastify.js', array('jquery'), PIX_VERSION, true );
    wp_enqueue_script( 'cocomsg', THEME_URL . '/inc/assets/js/coco-msg.js', array('jquery'), PIX_VERSION, true );
    wp_enqueue_script( 'timeago.js', THEME_URL . '/inc/assets/js/timeago.js', array('jquery'), PIX_VERSION, true );
    wp_enqueue_script( 'uikit.min.js', THEME_URL . '/inc/assets/js/uikit.min.js', array('jquery'), PIX_VERSION, true );
    wp_enqueue_script( 'fancybox.js', THEME_URL . '/inc/assets/js/fancybox.js', array('jquery'), PIX_VERSION, true );
    wp_enqueue_script( 'highlight.min.js', THEME_URL . '/inc/assets/js/highlight.min.js', array('jquery'), PIX_VERSION, true );
    wp_enqueue_script( 'jquery-confirm.min.js', THEME_URL . '/inc/assets/js/jquery-confirm.min.js', array('jquery'), PIX_VERSION, true );
    wp_enqueue_script( 'htmx.min.js', THEME_URL . '/inc/assets/js/htmx.min.js', array(), PIX_VERSION, true );
    wp_enqueue_script( 'swiper.js', THEME_URL . '/inc/assets/js/swiper-bundle.min.js', array('jquery'), PIX_VERSION, true );
    wp_enqueue_script( 'pix-uploader.js', THEME_URL . '/inc/assets/js/pix-uploader.js', array('jquery'), PIX_VERSION, true );
    wp_enqueue_script( 'user.js', THEME_URL . '/inc/assets/js/user.js', array('jquery', 'pix-uploader.js'), PIX_VERSION, true );
    wp_enqueue_script( 'moment.js', THEME_URL . '/inc/assets/js/moment.js', array('jquery', 'pix-uploader.js'), PIX_VERSION, true );
    

    // 验证码 - 根据设置按需加载
    $captcha_type = get_op('captcha_type', 'normal');
    if ($captcha_type == 'geetest') {
        wp_enqueue_script( 'geetest', 'https://static.geetest.com/v4/gt4.js', array(), PIX_VERSION, true );
    } elseif ($captcha_type == 'ppoc') {
        wp_enqueue_script( 'geetest', 'https://static.geetest.com/v4/gt4.js', array(), PIX_VERSION, true );
        wp_enqueue_script( 'ppo-captcha', THEME_URL . '/inc/assets/js/ppo-captcha.js', array('jquery', 'geetest'), PIX_VERSION, true );
        wp_enqueue_style( 'ppo-captcha', THEME_URL . '/inc/assets/css/ppo-captcha.css', array(), PIX_VERSION );
    } elseif ($captcha_type == 'code') {
        // 随机验证码 — 无需额外加载资源
    }

    // 登录 - 模态窗口，所有页面需要
    wp_enqueue_script( 'login', THEME_URL . '/inc/assets/js/login.js', array('jquery'), PIX_VERSION, true );


    // 文章详情页 - 分享海报需要 html2canvas
    if (is_singular()) {
        wp_enqueue_script( 'html2canvas.js', THEME_URL . '/inc/assets/js/html2canvas.min.js', array('jquery'), PIX_VERSION, true );
    }

    // 用户中心页面（/dashboard, /user/, /msg/ 等路由）
    /* $is_user_page = false;
    $request_uri = $_SERVER['REQUEST_URI'] ?? '';
    $path = parse_url($request_uri, PHP_URL_PATH);
    if (preg_match('#/dashboard#', $path) || preg_match('#/user/#', $path) || preg_match('#/msg/#', $path)) {
        $is_user_page = true;
    }
    if ($is_user_page) {
        
    } */

    // 文章页 - 视频播放器
    if (is_singular()) {
        global $post;
        if ($post && has_shortcode($post->post_content, 'video') || 
            has_shortcode($post->post_content, 'player') || 
            has_shortcode($post->post_content, 'ppo_video')) {
            wp_enqueue_script( 'artplayer.js', THEME_URL . '/inc/assets/js/artplayer.js', array('jquery'), PIX_VERSION, true );
            wp_enqueue_script( 'ppo-video', THEME_URL . '/inc/assets/js/ppo-video.js', array('jquery'), PIX_VERSION, true );
        }
    }

    // 鼠标光效 - 如果启用
    if (get_op('enable_cursor', false)) {
        wp_enqueue_script( 'gsap.js', 'https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/gsap/3.9.1/gsap.min.js', array(), PIX_VERSION, true );
        wp_enqueue_script( 'cursor.js', THEME_URL . '/inc/assets/js/cursor.js', array('jquery', 'gsap.js'), PIX_VERSION, true );
    }

    // 按需加载 - 需要时由其他模块调用
    // html2canvas.js, readmore.js, codeinput.js 等按需加载

    // Sticky Sidebar
    wp_enqueue_script( 'sticky-sidebar', THEME_URL . '/inc/assets/js/sticky-sidebar.js', array('jquery'), PIX_VERSION, true );
    wp_enqueue_script( 'smooth-scroll', THEME_URL . '/inc/assets/js/smooth-scroll.js', array('jquery'), PIX_VERSION, true );

    // 主应用入口
    wp_enqueue_script( 'app', THEME_URL . '/inc/assets/js/app.js', array(
        'jquery',
        'lazyload.min.js',
        'clipboard.min.js',
        'toastify',
        'cocomsg',
        'timeago.js',
        'uikit.min.js',
        'fancybox.js',
        'highlight.min.js',
        'htmx.min.js',
        'swiper.js',
        'jquery-confirm.min.js',
        'pix-uploader.js'
    ), PIX_VERSION, true );
	
    
   /*  if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		//wp_enqueue_script( 'comment-reply' );
	 } */
    
    wp_localize_script( 'app', 'Theme' , array(
	'ajaxurl' => admin_url( 'admin-ajax.php' ),
    'posts' => json_encode( $wp_query->query_vars ),
    'current_page' => get_query_var( 'paged' ) ? get_query_var('paged') : 1,
	'max_page' => $wp_query->max_num_pages,
    'redirecturl' => ppo_get_curl(),
    'ppo_url' => THEME_URL,
    'uid' => (int)get_current_user_id(),
    'de_ava' => THEME_URL.'/img/ava.png',
    'home_url' => home_url(),
    'nav_height' => cls_nav_height(),

    //comments
    'order' => get_option('comment_order'),
    'formpostion' => 'top',
    //'scroll_comment' => get_op('comment_nav','pagenav'),
    'pid' => (is_single() || is_page()) ? $post->ID : '',
    'tid' => get_queried_object()->term_id ?? false,
    'moment_nav' => get_op('moment_nav','btn'),
    'post_nav' => get_op('post_nav','btn'),
	'rest_nonce' => wp_create_nonce('wp_rest'),
	'moment_nonce' => wp_create_nonce('moment_ajax'),
	'post_nonce' => wp_create_nonce('post_ajax'),
	'upload_nonce' => wp_create_nonce('pix_upload_action'),
    'upload_settings' => array(
        'image_compress_enable' => (bool) get_op('image_compress_enable', true),
        'image_convert_webp' => (bool) get_op('image_convert_webp', false),
        'image_compress_quality' => max(1, min(100, (int) get_op('image_compress_quality', 86))),
        'image_compress_width' => get_op('image_compress_width', '1920'),
    ),
    'comment_upload' => array(
        'enabled' => (bool) get_op('comment_image_enable', true),
        'limit' => max(1, min(12, (int) get_op('comment_image_limit', 4))),
        'max_size' => max(1, min(20, (int) get_op('comment_image_max_size', 2))),
    ),
	'card_nonce' => wp_create_nonce('ppo_redeem_card'),
	'comt_nonce' => wp_create_nonce('upload_comment_img'),
	'user_nonce' => wp_create_nonce('ppo_user_action'),
	'oauth_nonce' => wp_create_nonce('ppo_oauth_action'),
	'pay_nonce' => wp_create_nonce('ppo_pay_action'),
	'msg_nonce' => wp_create_nonce('ppo_msg_action'),
	));
}
add_action( 'wp_enqueue_scripts', 'pix_scripts' );

// 优先替换 jQuery（priority 0），确保先于插件执行，同时保留 jquery handle 避免 noConflict 问题
add_action('wp_enqueue_scripts', function() {
    if (!is_admin()) {
        wp_deregister_script('jquery');
        wp_enqueue_script('jquery', THEME_URL . '/inc/assets/js/jquery.min.js', array(), PIX_VERSION, true);
    }
}, 0);

require THEME_DIR . '/inc/base.php';  //基础函数
require THEME_DIR . '/inc/global.php';  //全局函数
require THEME_DIR . '/inc/opt.php';  //加载优化函数
require THEME_DIR . '/inc/vendor/autoload.php'; //支付，curl,社交登录




