| 1 |
<?php |
| 2 |
/** |
| 3 |
* Description of ProductVideoTabController |
| 4 |
* |
| 5 |
* @author Ali2Woo Team |
| 6 |
* |
| 7 |
* @autoload: init |
| 8 |
* |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace AliNext_Lite;; |
| 12 |
|
| 13 |
class ProductVideoController extends AbstractController |
| 14 |
{ |
| 15 |
private string $videoUrl; |
| 16 |
private ?string $videoPoster; |
| 17 |
|
| 18 |
public const SHORTCODE_TAG_VIDEO = 'a2wl_product_video'; |
| 19 |
|
| 20 |
private ImportedProductServiceFactory $ImportedProductServiceFactory; |
| 21 |
|
| 22 |
public function __construct(ImportedProductServiceFactory $ImportedProductServiceFactory) |
| 23 |
{ |
| 24 |
parent::__construct(); |
| 25 |
|
| 26 |
$this->ImportedProductServiceFactory = $ImportedProductServiceFactory; |
| 27 |
|
| 28 |
add_action('init', [$this, 'initShortcode']); |
| 29 |
add_filter('woocommerce_product_tabs', [$this, 'addVideoTab']); |
| 30 |
} |
| 31 |
|
| 32 |
public function initShortcode(): void |
| 33 |
{ |
| 34 |
add_shortcode(self::SHORTCODE_TAG_VIDEO, [$this, 'handleVideoShortcode']); |
| 35 |
} |
| 36 |
|
| 37 |
public function handleVideoShortcode(array $attributes): string |
| 38 |
{ |
| 39 |
global $product; |
| 40 |
|
| 41 |
$args = shortcode_atts( |
| 42 |
[ |
| 43 |
'product_id' => '', |
| 44 |
'poster' => '', |
| 45 |
'loop' => '', |
| 46 |
'autoplay' => '', |
| 47 |
'preload' => 'metadata', |
| 48 |
'height' => false, |
| 49 |
'width' => false, |
| 50 |
'class' => 'wp-video-shortcode a2wl-product-video-shortcode', |
| 51 |
], $attributes |
| 52 |
); |
| 53 |
if ($args['height'] === false) { |
| 54 |
unset($args['height']); |
| 55 |
} |
| 56 |
if ($args['width'] === false) { |
| 57 |
unset( $args['width']); |
| 58 |
} |
| 59 |
if (!$args['product_id']) { |
| 60 |
if ($product) { |
| 61 |
$args['product_id'] = $product->get_id(); |
| 62 |
} |
| 63 |
} |
| 64 |
if ($args['product_id']) { |
| 65 |
$WC_Product = wc_get_product($args['product_id']); |
| 66 |
if (!$WC_Product) { |
| 67 |
return ''; |
| 68 |
} |
| 69 |
|
| 70 |
$ImportedProductService = $this->ImportedProductServiceFactory->createFromProduct($WC_Product); |
| 71 |
$videoData = $ImportedProductService->getVideoData(); |
| 72 |
|
| 73 |
if (!empty($videoData)) { |
| 74 |
$videoUrl = Utils::getProductVideoUrl(['video' => $videoData]); |
| 75 |
$videoPoster = Utils::getProductVideoPoster(['video' => $videoData]); |
| 76 |
if ($videoPoster) { |
| 77 |
$args['poster'] = $videoPoster; |
| 78 |
} |
| 79 |
|
| 80 |
unset($args['product_id']); |
| 81 |
$shortcodeAttributes = []; |
| 82 |
foreach ($args as $key => $value) { |
| 83 |
$shortcodeAttributes[] = $key . '="' . $value . '"'; |
| 84 |
} |
| 85 |
|
| 86 |
$content = sprintf( |
| 87 |
'[video src="%s" %s]', |
| 88 |
$videoUrl, |
| 89 |
implode( ' ', $shortcodeAttributes ) |
| 90 |
); |
| 91 |
|
| 92 |
return do_shortcode($content); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return ''; |
| 97 |
} |
| 98 |
|
| 99 |
public function addVideoTab(array $tabs): array |
| 100 |
{ |
| 101 |
$WC_Product = wc_get_product(get_the_ID()); |
| 102 |
if (!$WC_Product) { |
| 103 |
return $tabs; |
| 104 |
} |
| 105 |
|
| 106 |
$ImportedProductService = $this->ImportedProductServiceFactory->createFromProduct($WC_Product); |
| 107 |
|
| 108 |
$showVideoTabProduct = $ImportedProductService->getShouldShowVideoTab(); |
| 109 |
|
| 110 |
if (!$showVideoTabProduct) { |
| 111 |
if (!get_setting(Settings::SETTING_SHOW_PRODUCT_VIDEO_TAB)) { |
| 112 |
return $tabs; |
| 113 |
} |
| 114 |
} else { |
| 115 |
if ($showVideoTabProduct === ShouldShowVideoTab::HIDE) { |
| 116 |
return $tabs; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
$videoData = $ImportedProductService->getVideoData(); |
| 121 |
|
| 122 |
if (!empty($videoData)) { |
| 123 |
$this->videoUrl = Utils::getProductVideoUrl(['video' => $videoData]); |
| 124 |
$this->videoPoster = Utils::getProductVideoPoster(['video' => $videoData]); |
| 125 |
|
| 126 |
if ($this->videoUrl) { |
| 127 |
$tabs['a2wl_video_tab'] = [ |
| 128 |
'title' => __( 'Video', 'ali2woo' ), |
| 129 |
'priority' => get_setting(Settings::SETTING_VIDEO_TAB_PRIORITY), |
| 130 |
'callback' => [$this, 'renderVideoTab'] |
| 131 |
]; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
return $tabs; |
| 136 |
} |
| 137 |
|
| 138 |
public function renderVideoTab(): void |
| 139 |
{ |
| 140 |
$content = sprintf( |
| 141 |
'[video src="%s" poster="%s"]', |
| 142 |
$this->videoUrl, |
| 143 |
$this->videoPoster ?? 'none' |
| 144 |
); |
| 145 |
|
| 146 |
if (get_setting(Settings::SETTING_MAKE_VIDEO_FULL_TAB_WIDTH)) { |
| 147 |
$content = sprintf( |
| 148 |
'[video src="%s" poster="%s" width=""]', |
| 149 |
$this->videoUrl, |
| 150 |
$this->videoPoster ?? 'none' |
| 151 |
); |
| 152 |
} |
| 153 |
|
| 154 |
echo do_shortcode($content); |
| 155 |
} |
| 156 |
} |
| 157 |
|