| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Admin\Product\elements\SingleProduct; |
| 4 |
|
| 5 |
use SyncBasalam\Services\Products\VideoSourceResolver; |
| 6 |
use SyncBasalam\Utilities\ProductMetaKey; |
| 7 |
|
| 8 |
defined('ABSPATH') || exit; |
| 9 |
|
| 10 |
class VideoField |
| 11 |
{ |
| 12 |
private const META_BOX_ID = 'sync_basalam_product_video_box'; |
| 13 |
|
| 14 |
public static function shouldRender(): bool |
| 15 |
{ |
| 16 |
return VideoSourceResolver::isUsingPluginBox(); |
| 17 |
} |
| 18 |
|
| 19 |
public static function registerMetaBox(): void |
| 20 |
{ |
| 21 |
if (!self::shouldRender()) { |
| 22 |
return; |
| 23 |
} |
| 24 |
|
| 25 |
add_meta_box( |
| 26 |
self::META_BOX_ID, |
| 27 |
'ویدیو � |
| 28 |
حصول (باسلا� |
| 29 |
)', |
| 30 |
[self::class, 'render'], |
| 31 |
'product', |
| 32 |
'side', |
| 33 |
'low' |
| 34 |
); |
| 35 |
} |
| 36 |
|
| 37 |
public static function render(): void |
| 38 |
{ |
| 39 |
if (!self::shouldRender()) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
wp_enqueue_media(); |
| 44 |
|
| 45 |
$productId = (int) get_the_ID(); |
| 46 |
$metaKey = ProductMetaKey::basalamProductVideo(); |
| 47 |
$attachmentId = (int) get_post_meta($productId, $metaKey, true); |
| 48 |
$previewUrl = $attachmentId ? wp_get_attachment_url($attachmentId) : ''; |
| 49 |
|
| 50 |
wp_nonce_field('sync_basalam_save_video_field_action', '_sync_basalam_video_field_nonce'); |
| 51 |
|
| 52 |
echo '<div class="basalam-video-field">'; |
| 53 |
echo '<p style="color:#666;font-size:12px;margin:0 0 8px;">یک ویدیو از کتابخانه رسانهها انتخاب کنید تا هنگا� |
| 54 |
ه� |
| 55 |
گا� |
| 56 |
سازی با باسلا� |
| 57 |
ارسال شود.</p>'; |
| 58 |
|
| 59 |
echo '<input type="hidden" id="basalam_product_video_id" name="' . esc_attr($metaKey) . '" value="' . esc_attr((string) $attachmentId) . '">'; |
| 60 |
|
| 61 |
echo '<div id="basalam_product_video_preview" style="margin-bottom:8px;">'; |
| 62 |
if ($previewUrl) { |
| 63 |
echo '<video src="' . esc_url($previewUrl) . '" controls style="max-width:100%;height:auto;display:block;"></video>'; |
| 64 |
} |
| 65 |
echo '</div>'; |
| 66 |
|
| 67 |
echo '<button type="button" class="button button-secondary" id="basalam_product_video_select">'; |
| 68 |
echo $attachmentId ? 'تغییر ویدیو' : 'انتخاب ویدیو'; |
| 69 |
echo '</button> '; |
| 70 |
|
| 71 |
echo '<button type="button" class="button-link delete" id="basalam_product_video_remove" style="' . ($attachmentId ? '' : 'display:none;') . '">حذف ویدیو</button>'; |
| 72 |
|
| 73 |
echo '</div>'; |
| 74 |
|
| 75 |
self::renderInlineScript(); |
| 76 |
} |
| 77 |
|
| 78 |
public static function save(int $postId): void |
| 79 |
{ |
| 80 |
if ( |
| 81 |
!isset($_POST['_sync_basalam_video_field_nonce']) |
| 82 |
|| !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_sync_basalam_video_field_nonce'])), 'sync_basalam_save_video_field_action') |
| 83 |
) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
if (!VideoSourceResolver::isUsingPluginBox()) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
$metaKey = ProductMetaKey::basalamProductVideo(); |
| 92 |
$value = isset($_POST[$metaKey]) ? absint(wp_unslash($_POST[$metaKey])) : 0; |
| 93 |
|
| 94 |
if ($value > 0) { |
| 95 |
update_post_meta($postId, $metaKey, $value); |
| 96 |
} else { |
| 97 |
delete_post_meta($postId, $metaKey); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
private static function renderInlineScript(): void |
| 102 |
{ |
| 103 |
?> |
| 104 |
<script> |
| 105 |
(function ($) { |
| 106 |
$(function () { |
| 107 |
var frame; |
| 108 |
var $idInput = $('#basalam_product_video_id'); |
| 109 |
var $preview = $('#basalam_product_video_preview'); |
| 110 |
var $removeBtn = $('#basalam_product_video_remove'); |
| 111 |
var $selectBtn = $('#basalam_product_video_select'); |
| 112 |
|
| 113 |
$selectBtn.on('click', function (e) { |
| 114 |
e.preventDefault(); |
| 115 |
|
| 116 |
if (frame) { |
| 117 |
frame.open(); |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
frame = wp.media({ |
| 122 |
title: 'انتخاب ویدیو � |
| 123 |
حصول', |
| 124 |
button: { text: 'استفاده از این ویدیو' }, |
| 125 |
library: { type: 'video' }, |
| 126 |
multiple: false |
| 127 |
}); |
| 128 |
|
| 129 |
frame.on('select', function () { |
| 130 |
var attachment = frame.state().get('selection').first().toJSON(); |
| 131 |
$idInput.val(attachment.id); |
| 132 |
$preview.html('<video src="' + attachment.url + '" controls style="max-width:100%;height:auto;display:block;"></video>'); |
| 133 |
$removeBtn.show(); |
| 134 |
$selectBtn.text('تغییر ویدیو'); |
| 135 |
}); |
| 136 |
|
| 137 |
frame.open(); |
| 138 |
}); |
| 139 |
|
| 140 |
$removeBtn.on('click', function (e) { |
| 141 |
e.preventDefault(); |
| 142 |
$idInput.val(''); |
| 143 |
$preview.empty(); |
| 144 |
$removeBtn.hide(); |
| 145 |
$selectBtn.text('انتخاب ویدیو'); |
| 146 |
}); |
| 147 |
}); |
| 148 |
})(jQuery); |
| 149 |
</script> |
| 150 |
<?php |
| 151 |
} |
| 152 |
} |
| 153 |
|