| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Services\Products; |
| 4 |
|
| 5 |
use SyncBasalam\Admin\Settings\SettingsConfig; |
| 6 |
use SyncBasalam\Utilities\ProductMetaKey; |
| 7 |
|
| 8 |
defined('ABSPATH') || exit; |
| 9 |
|
| 10 |
class VideoSourceResolver |
| 11 |
{ |
| 12 |
private const AUTO_DETECT_META_KEYS = [ |
| 13 |
'_woodmart_product_video', |
| 14 |
'_woodmart_product_video_gallery', |
| 15 |
'_product_video', |
| 16 |
'_product_video_gallery', |
| 17 |
'_yith_wcpv_product_video', |
| 18 |
'_elementor_video_url', |
| 19 |
'_xstore_product_video', |
| 20 |
'featured_video', |
| 21 |
'_featured_video', |
| 22 |
]; |
| 23 |
|
| 24 |
public static function resolveMetaKey(): ?string |
| 25 |
{ |
| 26 |
$settings = syncBasalamSettings(); |
| 27 |
$source = $settings->getSettings(SettingsConfig::VIDEO_SOURCE); |
| 28 |
|
| 29 |
if ($source === 'plugin_box') { |
| 30 |
return ProductMetaKey::basalamProductVideo(); |
| 31 |
} |
| 32 |
|
| 33 |
$mode = $settings->getSettings(SettingsConfig::VIDEO_INHERIT_MODE); |
| 34 |
|
| 35 |
if ($mode === 'manual') { |
| 36 |
$key = trim((string) $settings->getSettings(SettingsConfig::VIDEO_META_KEY)); |
| 37 |
|
| 38 |
return $key !== '' ? $key : null; |
| 39 |
} |
| 40 |
|
| 41 |
return null; |
| 42 |
} |
| 43 |
|
| 44 |
public static function resolveValue(int $productId): ?string |
| 45 |
{ |
| 46 |
$settings = syncBasalamSettings(); |
| 47 |
$source = $settings->getSettings(SettingsConfig::VIDEO_SOURCE); |
| 48 |
|
| 49 |
if ($source === 'plugin_box') { |
| 50 |
return self::readMeta($productId, ProductMetaKey::basalamProductVideo()); |
| 51 |
} |
| 52 |
|
| 53 |
$mode = $settings->getSettings(SettingsConfig::VIDEO_INHERIT_MODE); |
| 54 |
|
| 55 |
if ($mode === 'manual') { |
| 56 |
$key = trim((string) $settings->getSettings(SettingsConfig::VIDEO_META_KEY)); |
| 57 |
|
| 58 |
return $key !== '' ? self::readMeta($productId, $key) : null; |
| 59 |
} |
| 60 |
|
| 61 |
foreach (self::AUTO_DETECT_META_KEYS as $key) { |
| 62 |
$value = self::readMeta($productId, $key); |
| 63 |
|
| 64 |
if ($value !== null) { |
| 65 |
return $value; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
return null; |
| 70 |
} |
| 71 |
|
| 72 |
public static function isUsingPluginBox(): bool |
| 73 |
{ |
| 74 |
$source = syncBasalamSettings()->getSettings(SettingsConfig::VIDEO_SOURCE); |
| 75 |
|
| 76 |
return $source === 'plugin_box'; |
| 77 |
} |
| 78 |
|
| 79 |
public static function autoDetectMetaKeys(): array |
| 80 |
{ |
| 81 |
return self::AUTO_DETECT_META_KEYS; |
| 82 |
} |
| 83 |
|
| 84 |
private static function readMeta(int $productId, string $key): ?string |
| 85 |
{ |
| 86 |
$value = get_post_meta($productId, $key, true); |
| 87 |
|
| 88 |
if (is_array($value)) { |
| 89 |
$value = reset($value); |
| 90 |
} |
| 91 |
|
| 92 |
if ($value === '' || $value === null || $value === false) { |
| 93 |
return null; |
| 94 |
} |
| 95 |
|
| 96 |
return (string) $value; |
| 97 |
} |
| 98 |
} |
| 99 |
|