<?php
/*
Theme Name: Hello Elementor Child
Theme URI: https://elementor.com/hello-theme/?utm_source=wp-themes&utm_campaign=theme-uri&utm_medium=wp-dash
Template: hello-elementor
Author: Elementor Team
Author URI: https://elementor.com/?utm_source=wp-themes&utm_campaign=author-uri&utm_medium=wp-dash
Description: Hello Elementor is a lightweight and minimalist WordPress theme that was built specifically to work seamlessly with the Elementor site builder plugin. The theme is free, open-source, and designed for users who want a flexible, easy-to-use, and customizable website. The theme, which is optimized for performance, provides a solid foundation for users to build their own unique designs using the Elementor drag-and-drop site builder. Its simplicity and flexibility make it a great choice for both beginners and experienced Web Creators.
Tags: accessibility-ready,flexible-header,custom-colors,custom-menu,custom-logo,featured-images,rtl-language-support,threaded-comments,translation-ready
Version: 3.4.4.1756787630
Updated: 2025-09-02 13:33:50
*/

// 子テーマスタイル読み込み
function hello_elementor_child_enqueue_scripts() {
    wp_enqueue_style('hello-elementor-child-style', get_stylesheet_directory_uri() . '/style.css', array('hello-elementor-theme-style'), wp_get_theme()->get('Version'));
}
add_action('wp_enqueue_scripts', 'hello_elementor_child_enqueue_scripts');

// PeerJSエラー抑制
function suppress_peerjs_errors() {
    if (is_cart()) {
        ?>
        <script>
        // PeerJSエラーを抑制
        window.addEventListener('error', function(e) {
            if (e.message && e.message.includes('PeerJS')) {
                e.preventDefault();
                return false;
            }
        });
        
        // コンソールエラーを一時的に抑制
        var originalError = console.error;
        console.error = function() {
            var args = Array.prototype.slice.call(arguments);
            if (args.some(arg => typeof arg === 'string' && arg.includes('PeerJS'))) {
                return; // PeerJSエラーは表示しない
            }
            originalError.apply(console, args);
        };
        </script>
        <?php
    }
}
add_action('wp_head', 'suppress_peerjs_errors', 1);

// カートページナビゲーション - エラー回避版
function error_safe_cart_navigation() {
    if (!is_cart()) return;
    
    try {
        ?>
        <script>
        (function() {
            'use strict';
            
            console.log('🎯 エラー回避版カートナビゲーション開始');
            
            function addCartNavigation() {
                try {
                    // 既存チェック
                    if (document.getElementById('safe-cart-nav')) {
                        console.log('✅ 既に表示済み');
                        return;
                    }
                    
                    // より安全な要素検索
                    var targetSelectors = [
                        '.woocommerce-cart',
                        'form[action*="cart"]',
                        '.site-main',
                        'main',
                        '#main',
                        '.content-area'
                    ];
                    
                    var targetElement = null;
                    for (var i = 0; i < targetSelectors.length; i++) {
                        try {
                            targetElement = document.querySelector(targetSelectors[i]);
                            if (targetElement) {
                                console.log('✅ ターゲット発見: ' + targetSelectors[i]);
                                break;
                            }
                        } catch (e) {
                            console.log('⚠️ セレクターエラー: ' + targetSelectors[i]);
                        }
                    }
                    
                    if (targetElement) {
                        var navHTML = [
                            '<div id="safe-cart-nav" style="',
                                'background: linear-gradient(135deg, #FF6B6B 0%, #4ECDC4 100%);',
                                'color: white;',
                                'padding: 25px;',
                                'text-align: center;',
                                'margin: 25px 0;',
                                'border-radius: 12px;',
                                'box-shadow: 0 6px 20px rgba(0,0,0,0.15);',
                                'font-family: -apple-system, BlinkMacSystemFont, sans-serif;',
                                'position: relative;',
                                'overflow: hidden;',
                            '">',
                            '<div style="position: relative; z-index: 2;">',
                                '<h3 style="margin: 0 0 20px 0; font-size: 20px; font-weight: 600;">🎬 支援プランを見直しませんか？</h3>',
                                '<div style="display: flex; justify-content: center; gap: 15px; flex-wrap: wrap;">',
                                    '<a href="/asuyare/" style="',
                                        'background: rgba(255,255,255,0.9);',
                                        'color: #FF6B6B;',
                                        'padding: 14px 28px;',
                                        'border-radius: 8px;',
                                        'text-decoration: none;',
                                        'font-weight: 600;',
                                        'display: inline-block;',
                                        'transition: all 0.3s ease;',
                                        'border: 2px solid transparent;',
                                    '">← 支援プランを選び直す</a>',
                                    '<a href="/shop/" style="',
                                        'background: rgba(255,255,255,0.9);',
                                        'color: #4ECDC4;',
                                        'padding: 14px 28px;',
                                        'border-radius: 8px;',
                                        'text-decoration: none;',
                                        'font-weight: 600;',
                                        'display: inline-block;',
                                        'transition: all 0.3s ease;',
                                        'border: 2px solid transparent;',
                                    '">他の商品も見る</a>',
                                '</div>',
                            '</div>',
                            '</div>'
                        ].join('');
                        
                        targetElement.insertAdjacentHTML('afterbegin', navHTML);
                        console.log('✅ ナビゲーション挿入成功');
                        
                        // ホバーエフェクト追加
                        setTimeout(function() {
                            var links = document.querySelectorAll('#safe-cart-nav a');
                            links.forEach(function(link) {
                                link.addEventListener('mouseenter', function() {
                                    this.style.transform = 'translateY(-3px) scale(1.05)';
                                    this.style.boxShadow = '0 8px 25px rgba(0,0,0,0.2)';
                                });
                                link.addEventListener('mouseleave', function() {
                                    this.style.transform = 'translateY(0) scale(1)';
                                    this.style.boxShadow = 'none';
                                });
                            });
                        }, 100);
                        
                    } else {
                        console.log('❌ ターゲット要素が見つかりません');
                        
                        // 最終手段：body の先頭に固定配置
                        var fallbackHTML = [
                            '<div id="safe-cart-nav" style="',
                                'background: #FF6B6B;',
                                'color: white;',
                                'padding: 15px;',
                                'text-align: center;',
                                'position: fixed;',
                                'top: 0;',
                                'left: 0;',
                                'right: 0;',
                                'z-index: 99999;',
                                'box-shadow: 0 2px 10px rgba(0,0,0,0.3);',
                            '">',
                            '🎬 <a href="/asuyare/" style="color: white; margin: 0 10px;">支援プランを選び直す</a> | ',
                            '<a href="/shop/" style="color: white; margin: 0 10px;">他の商品も見る</a>',
                            '</div>'
                        ].join('');
                        
                        document.body.insertAdjacentHTML('afterbegin', fallbackHTML);
                        console.log('✅ フォールバック表示成功');
                    }
                    
                } catch (error) {
                    console.error('❌ カートナビゲーションエラー:', error);
                }
            }
            
            // 複数のタイミングで実行
            if (document.readyState === 'loading') {
                document.addEventListener('DOMContentLoaded', addCartNavigation);
            } else {
                addCartNavigation();
            }
            
            window.addEventListener('load', addCartNavigation);
            setTimeout(addCartNavigation, 1000);
            setTimeout(addCartNavigation, 3000);
            
        })();
        </script>
        <?php
    } catch (Exception $e) {
        error_log('Cart navigation error: ' . $e->getMessage());
    }
}
add_action('wp_footer', 'error_safe_cart_navigation', 999);

// カートページタイトル変更
function safe_cart_title($title, $id) {
    if (is_cart() && in_the_loop() && is_main_query()) {
        return '🎬 支援内容の確認';
    }
    return $title;
}
add_filter('the_title', 'safe_cart_title', 10, 2);

// チェックアウトボタンの文言変更
function safe_checkout_button() {
    return '🎬 支援を確定する';
}
add_filter('woocommerce_proceed_to_checkout_button_text', 'safe_checkout_button');

// 商品ページのボタン文言変更
function safe_add_to_cart_text($text) {
    global $product;
    
    if ($product && $product->is_type('simple')) {
        return '🎬 支援する';
    }
    
    return $text;
}
add_filter('woocommerce_product_add_to_cart_text', 'safe_add_to_cart_text');
/* 予告編：16:9の比率でレスポンシブ対応 */
.trailer-video .elementor-widget-container {
  position: relative;
  width: 100%;
  max-width: 960px;   /* 必要に応じて変更 */
  margin: 0 auto;
}

.trailer-video .elementor-widget-container::before {
  content: "";
  display: block;
  padding-top: 56.25%; /* 16:9 */
}

.trailer-video iframe {
  position: absolute;
  inset: 0;
  width: 100% !important;
  height: 100% !important;
  display: block;
}

