اضافه کردن ویدیو آپارات به گالری محصول

این کد امکان افزودن ویدیو آپارات به محصولات ووکامرس و نمایش آن در یک پاپ‌آپ اختصاصی را بدون نیاز به افزونه فراهم می‌کند.

اضافه کردن ویدیو آپارات به گالری صفحه محصول

این کد امکان اضافه کردن ویدیو آپارات به صفحه محصول ووکامرس را فراهم می‌کند. در بخش ویرایش محصول، فیلدی برای وارد کردن لینک ویدیو آپارات ایجاد می‌شود و لینک ذخیره‌شده برای نمایش ویدیو استفاده خواهد شد.

با شورت‌کد [aparat_video_button] یک دکمه «ویدیو» در صفحه محصول نمایش داده می‌شود. با کلیک روی این دکمه، ویدیو در یک پنجره پاپ‌آپ (Modal) باز شده و به‌صورت Embed از آپارات پخش می‌شود.

پاپ‌آپ قابلیت بستن با دکمه ×، کلیک روی فضای بیرونی و کلید Escape را دارد و هنگام باز بودن، اسکرول صفحه نیز غیرفعال می‌شود.

نحوه استفاده:
ابتدا لینک ویدیو را در فیلد «لینک ویدیو آپارات» محصول وارد کنید، سپس شورت‌کد زیر را در محل موردنظر صفحه محصول قرار دهید:

[aparat_video_button]


<?php
add_action( 'woocommerce_product_options_general_product_data', 'aparat_embed_field' );
function aparat_embed_field() {
    echo '<div class="options_group">';
    woocommerce_wp_text_input( array(
        'id'          => 'aparat_embed_url',
        'label'       => 'لینک ویدیو آپارات',
        'placeholder' => 'https://www.aparat.com/v/sxsxif1',
        'desc_tip'    => true,
        'description' => 'لینک معمولی ویدیو آپارات را وارد کنید.',
        'type'        => 'url'
    ) );
    echo '</div>';
}
add_action( 'woocommerce_process_product_meta', 'aparat_save_embed_field' );
function aparat_save_embed_field( $post_id ) {
    $embed = isset( $_POST['aparat_embed_url'] ) ? esc_url_raw( $_POST['aparat_embed_url'] ) : '';
    update_post_meta( $post_id, 'aparat_embed_url', $embed );
}
function aparat_extract_video_id( $url ) {
    if ( preg_match( '#aparat\.com/v/([A-Za-z0-9]+)#i', $url, $m ) ) {
        return $m[1]; }
    if ( preg_match( '#videohash/([A-Za-z0-9]+)#i', $url, $m ) ) {
        return $m[1]; }
    return false; }
function aparat_get_embed_url( $url ) {
    $hash = aparat_extract_video_id( $url );
    if ( ! $hash ) {
        return ''; }
    return 'https://www.aparat.com/video/video/embed/videohash/' . $hash . '/vt/frame?isamp'; }
function aparat_get_video_info( $url ) {
    $hash = aparat_extract_video_id( $url );
    if ( ! $hash ) {
        return false; }
    $cache_key = 'aparat_gallery_v_' . md5( $hash );
    $cached = get_transient( $cache_key );
    if ( $cached ) {
        return $cached; }
    $response = wp_remote_get(
        'https://www.aparat.com/api/fa/v1/video/video/show/videohash/' . rawurlencode( $hash ),
        array(
            'timeout' => 12,
        )
    );
    if ( is_wp_error( $response ) ) {
        return false; }
    $body = json_decode( wp_remote_retrieve_body( $response ), true );
    if ( empty( $body['data']['attributes'] ) ) {
        return false; }
    $attr = $body['data']['attributes'];
    $thumb = '';
    if ( ! empty( $attr['big_poster'] ) ) {
        $thumb = $attr['big_poster'];
    } elseif ( ! empty( $attr['poster'] ) ) {
        $thumb = $attr['poster'];
    } elseif ( ! empty( $attr['small_poster'] ) ) {
        $thumb = $attr['small_poster'];
    }
    if ( empty( $thumb ) ) { $thumb = 'https://www.aparat.com/v/' . $hash . '/thumbnail'; }
    $info = array(
        'thumb' => esc_url_raw( $thumb ),
        'title' => ! empty( $attr['title'] ) ? sanitize_text_field( $attr['title'] ) : ''
    );
    set_transient( $cache_key, $info, HOUR_IN_SECONDS );
    return $info; }
add_shortcode( 'aparat_video_button', 'aparat_video_button_shortcode' );
function aparat_video_button_shortcode() {
    if ( ! is_product() ) {
        return '';     }
    global $product;
    $url = get_post_meta( $product->get_id(), 'aparat_embed_url', true );
    if ( empty( $url ) ) {
        return ''; }
    $embed_url = aparat_get_embed_url( $url );
    if ( empty( $embed_url ) ) {
        return ''; }
    add_action( 'wp_footer', 'aparat_modal_html', 20 );
    return '
    <button type="button" class="aparat-video-btn" data-embed="' . esc_url( $embed_url ) . '" style="
        display:inline-flex;
        align-items:center;
        justify-content:center;
        gap:4px;
        padding:3px 3px 3px 6px;
        background:#ff5722;
        color:#fff;
        border:none;
        border-radius:5px;
        cursor:pointer;
        font-size:13px;
        font-weight:500;
        line-height:1;
    ">
<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="currentColor">
  <path d="M18 4H6C4.9 4 4 4.9 4 6V18C4 19.1 4.9 20 6 20H18C19.1 20 20 19.1 20 18V6C20 4.9 19.1 4 18 4ZM10 16V8L16 12L10 16Z"/>
</svg>
        ویدیو
    </button>';
}
function aparat_modal_html() {
    if ( did_action( 'aparat_modal_rendered' ) ) {
        return; }
    do_action( 'aparat_modal_rendered' );
    ?>
<style>
#aparat-modal{ display:none; position:fixed; left:0; top:0; width:100%; height:100%; background:rgba(0,0,0,.95); z-index:999999; align-items:center; justify-content:center; }
#aparat-modal-content{ position:relative; width:90%; max-width:900px; background:#000; border-radius:10px; overflow:hidden; }
#aparat-modal-close{ position:fixed; top:20px; right:20px; width:50px; height:50px; border-radius:50%; background:rgba(0,0,0,.6); display:flex; align-items:center; justify-content:center; font-size:40px; color:#fff; cursor:pointer; z-index:1000000; }
#iframe-wrapper{ position:relative; padding-bottom:56.25%; height:0; }
#iframe-wrapper iframe{ position:absolute; left:0; top:0; width:100%; height:100%; border:0; }
</style>
<div id="aparat-modal">
    <span id="aparat-modal-close">&times;</span>
    <div id="aparat-modal-content">
        <div id="iframe-wrapper">
            <iframe src="" allowfullscreen allow="autoplay; encrypted-media"></iframe>
        </div>
    </div>
</div>
<script>
jQuery(function($){
    $(document).on('click','.aparat-video-btn',function(e){
        e.preventDefault();
        var url=$(this).data('embed');
        $('#iframe-wrapper iframe').attr('src',url);
        $('#aparat-modal').css('display','flex').hide().fadeIn(200);
        $('body').css('overflow','hidden');
    });
    function closeModal(){
        $('#aparat-modal').fadeOut(200);
        $('#iframe-wrapper iframe').attr('src','');
        $('body').css('overflow','');
    }
    $('#aparat-modal-close').on('click',closeModal);
    $('#aparat-modal').on('click',function(e){
        if(e.target===this){
            closeModal();
        }
    });
    $(document).on('keydown',function(e){
        if(e.key==="Escape"){
            closeModal();
        }
    });
});
</script>
<?php
}

روش اول: فایل functions.php

اگر از قالب فرزند (Child Theme) استفاده می‌کنید، می‌توانید کد را در فایل: functions.php قالب فرزند قرار دهید.

توصیه می‌شود کد را مستقیماً در فایل functions.php قالب اصلی قرار ندهید؛ زیرا با به‌روزرسانی قالب ممکن است تغییرات شما از بین برود.

روش دوم: استفاده از افزونه Code Snippets

روش ساده‌تر این است که از افزونه‌های مدیریت Snippet استفاده کنید.

در این روش، کد را به‌عنوان یک Snippet جدید اضافه کرده و آن را فعال می‌کنید.

مزیت این روش این است که برای اضافه کردن کدهای PHP نیازی به ویرایش مستقیم فایل‌های قالب ندارید.

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *