/* 
Theme Name: Hello Elementor Child
Theme URI: https://github.com/elementor/hello-theme-child/
Description: Hello Elementor Child is a child theme of Hello Elementor, created by Elementor team
Author: Elementor Team
Author URI: https://elementor.com/
Template: hello-elementor
Version: 2.0.0
Text Domain: hello-elementor-child
License: GNU General Public License v3 or later.
License URI: https://www.gnu.org/licenses/gpl-3.0.html
Tags: flexible-header, custom-colors, custom-menu, custom-logo, editor-style, featured-images, rtl-language-support, threaded-comments, translation-ready
*/

/* Add your custom styles here */


add_action('woocommerce_product_options_general_product_data', 'beban_add_video_link_field');
function beban_add_video_link_field() {
    echo '<div class="options_group">';
    woocommerce_wp_textarea_input(array(
        'id' => '_product_video_iframe',
        'label' => __('کد ویدیو (iframe)', 'beban'),
        'placeholder' => '<iframe src="..."></iframe>',
        'description' => __('کد iframe ویدیو محصول (آپارات، یوتیوب، و...)', 'beban'),
        'desc_tip' => true,
        'rows' => 6,
    ));
    echo '</div>';
}

/**
 * Save video iframe code field
 */
add_action('woocommerce_process_product_meta', 'beban_save_video_link_field');
function beban_save_video_link_field($post_id) {
    if (!isset($_POST['_product_video_iframe'])) {
        return;
    }
    
    // Save iframe code with minimal sanitization (only unslash)
    $video_iframe = wp_unslash($_POST['_product_video_iframe']);
    update_post_meta($post_id, '_product_video_iframe', $video_iframe);
}

/**
 * Get product video iframe code
 */
function beban_get_product_video_iframe($product_id = null) {
    if (!$product_id) {
        global $post;
        $product_id = $post ? $post->ID : 0;
    }
    
    return get_post_meta($product_id, '_product_video_iframe', true);
}

/**
 * Shortcode: [product_video]
 */
add_shortcode('product_video', 'beban_product_video_shortcode');
function beban_product_video_shortcode($atts) {
    $atts = shortcode_atts(array(
        'id' => null,
    ), $atts);
    
    $product_id = $atts['id'];
    if (!$product_id) {
        global $post;
        $product_id = $post ? $post->ID : 0;
    }
    
    // Check if we're in a product context
    if (get_post_type($product_id) !== 'product') {
        return '';
    }
    
    $iframe_code = beban_get_product_video_iframe($product_id);
    
    // Start output buffering
    ob_start();
    
    if (empty($iframe_code)) {
        ?>
        <div class="beban-no-video-message" style="text-align: center; padding: 40px 20px;">
            <svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" width="120" height="120" x="0" y="0" viewBox="0 0 68 68" style="enable-background:new 0 0 512 512; margin: 0 auto; display: block;" xml:space="preserve">
                <g><path d="M21.5 38.6c.3-.3.3-.8 0-1.1l-2.4-2.4 3.1-3.1c.3-.3.3-.8 0-1.1s-.8-.3-1.1 0L18 34l-2.8-2.8c-.3-.3-.8-.3-1.1 0s-.3.8 0 1.1l2.8 2.8-2.8 2.8c-.3.3-.3.8 0 1.1s.8.3 1.1 0l2.8-2.8 2.4 2.4c.4.3.8.3 1.1 0zM21.2 48.5c-.3.3-.3.8.1 1.1.3.3.8.3 1.1-.1 4.9-5.4 13.3-5.3 18.1-.3.3.3.8.3 1.1 0s.3-.8 0-1.1c-5.5-5.6-14.9-5.7-20.4.4z" fill="#C4C4C4" opacity="1"></path><path d="M58.6 54.5 61.2 28c.1-1.1-.3-2.2-1-3s-1.8-1.3-2.9-1.3v-6.9c0-2.6-2-4.7-4.6-4.8l-42.1-.6c-2.7 0-4.7 2.1-4.8 4.6v1.8c-1.2.3-2.1 1.1-2.6 2.3-.6.7-1 1.6-1.2 2.3-.5 1.8-.4 3.7-.2 5.6.8 8.8 1.6 17.5 2.3 26.3.3 3.8 1.3 5.4 6.8 5.7 15.7.8 31.4.7 47.1.4.6 0 1.3 0 1.8-.3.6-.3 1-.9.9-1.5-.3-1.3-2.5-1.1-3.8-1.2.9-.6 1.5-1.6 1.7-2.9zM10.6 13l42.1.6c1.7 0 3.2 1.5 3.1 3.2v7H41.1c-1.1 0-2.2-.5-3-1.3l-2.3-2.6c-1.2-1.3-2.9-2.1-4.7-2.1H7.3v-1.6c0-1.9 1.5-3.3 3.3-3.2zm44.1 43.6H9.6c-1.2 0-2.3-.9-2.4-2.2L4.3 21.8c-.1-1.4 1-2.6 2.4-2.6h24.4c1.4 0 2.7.6 3.6 1.6l2.3 2.6c1 1.2 2.5 1.9 4.1 1.9h16.2c.7 0 1.3.3 1.8.8s.7 1.2.6 1.8l-2.6 26.5c-.1 1.2-1.2 2.2-2.4 2.2z" fill="#C4C4C4" opacity="1"></path><path d="m44.2 33.8 2.2-2.2c.3-.3.3-.8 0-1.1s-.8-.3-1.1 0l-2.2 2.2-2.8-2.8c-.3-.3-.8-.3-1.1 0s-.3.8 0 1.1l2.8 2.8-2.6 2.6c-.3.3-.3.8 0 1.1s.8.3 1.1 0l2.6-2.6 2.8 2.8c.3.3.8.3 1.1 0s.3-.8 0-1.1zM57.8 7.6c-.9 1.2-1.6 2.4-2.1 3.8-.1.4.1.8.5.9s.8-.1.9-.5c.4-1.2 1-2.4 1.8-3.4.3-.3.2-.8-.1-1.1-.3 0-.7 0-1 .3zM64.2 10.5c-.2-.3-.7-.5-1-.2l-4.7 3c-.3.2-.4.7-.2 1s.7.5 1 .2l4.7-3c.3-.2.4-.7.2-1zM65.6 15.9l-5.3.4c-.4 0-.7.4-.7.8s.4.7.8.7l5.3-.4c.4 0 .7-.4.7-.8-.1-.4-.4-.7-.8-.7z" fill="#C4C4C4" opacity="1"></path></g>
            </svg>
            <p style="color: #cbcbcb; margin-top: 20px; font-size: 16px;">در حال حاضر ویدیویی برای این محصول وجود ندارد</p>
        </div>
        <?php
    } else {
        ?>
        <div class="beban-product-video-wrapper" style="border-radius: 24px; overflow: hidden;">
            <?php echo $iframe_code; // Output iframe code as-is (includes its own styling) ?>
        </div>
        <?php
    }
    
    return ob_get_clean();
}