| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Utilities; |
| 4 |
|
| 5 |
use SyncBasalam\Admin\Settings\SettingsConfig; |
| 6 |
|
| 7 |
defined('ABSPATH') || exit; |
| 8 |
|
| 9 |
class ProductMetaKey |
| 10 |
{ |
| 11 |
public const PRODUCT_ID = 'sync_basalam_product_id'; |
| 12 |
public const PRODUCT_SYNC_STATUS = 'sync_basalam_product_sync_status'; |
| 13 |
public const PRODUCT_STATUS = 'sync_basalam_product_status'; |
| 14 |
public const PRODUCT_VIDEO = '_sync_basalam_product_video'; |
| 15 |
|
| 16 |
public static function basalamProductVideo(): string |
| 17 |
{ |
| 18 |
return self::PRODUCT_VIDEO; |
| 19 |
} |
| 20 |
|
| 21 |
public static function basalamProductId($vendorId = null): string |
| 22 |
{ |
| 23 |
return self::build(self::PRODUCT_ID, $vendorId); |
| 24 |
} |
| 25 |
|
| 26 |
public static function basalamProductSyncStatus($vendorId = null): string |
| 27 |
{ |
| 28 |
return self::build(self::PRODUCT_SYNC_STATUS, $vendorId); |
| 29 |
} |
| 30 |
|
| 31 |
public static function basalamProductStatus($vendorId = null): string |
| 32 |
{ |
| 33 |
return self::build(self::PRODUCT_STATUS, $vendorId); |
| 34 |
} |
| 35 |
|
| 36 |
public static function basalamProductMetaKeys($vendorId = null): array |
| 37 |
{ |
| 38 |
return [ |
| 39 |
self::basalamProductId($vendorId), |
| 40 |
self::basalamProductSyncStatus($vendorId), |
| 41 |
self::basalamProductStatus($vendorId), |
| 42 |
]; |
| 43 |
} |
| 44 |
|
| 45 |
private static function build(string $baseKey, $vendorId = null): string |
| 46 |
{ |
| 47 |
$resolvedVendorId = self::normalizeVendorId($vendorId ?? self::getVendorIdFromSettings()); |
| 48 |
|
| 49 |
if ($resolvedVendorId === '') { |
| 50 |
return $baseKey; |
| 51 |
} |
| 52 |
|
| 53 |
return "{$baseKey}_{$resolvedVendorId}"; |
| 54 |
} |
| 55 |
|
| 56 |
private static function getVendorIdFromSettings() |
| 57 |
{ |
| 58 |
return syncBasalamSettings()->getSettings(SettingsConfig::VENDOR_ID); |
| 59 |
} |
| 60 |
|
| 61 |
private static function normalizeVendorId($vendorId): string |
| 62 |
{ |
| 63 |
if ($vendorId === null || $vendorId === '') { |
| 64 |
return ''; |
| 65 |
} |
| 66 |
|
| 67 |
return preg_replace('/[^a-zA-Z0-9_-]/', '', (string) $vendorId); |
| 68 |
} |
| 69 |
} |
| 70 |
|