| 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 |
public const VARIATION_ID = 'sync_basalam_variation_id'; |
| 16 |
public const DISCOUNTED = 'sync_basalam_discounted'; |
| 17 |
|
| 18 |
public static function basalamProductVideo(): string |
| 19 |
{ |
| 20 |
return self::PRODUCT_VIDEO; |
| 21 |
} |
| 22 |
|
| 23 |
public static function basalamProductId($vendorId = null): string |
| 24 |
{ |
| 25 |
return self::build(self::PRODUCT_ID, $vendorId); |
| 26 |
} |
| 27 |
|
| 28 |
public static function basalamProductSyncStatus($vendorId = null): string |
| 29 |
{ |
| 30 |
return self::build(self::PRODUCT_SYNC_STATUS, $vendorId); |
| 31 |
} |
| 32 |
|
| 33 |
public static function basalamProductStatus($vendorId = null): string |
| 34 |
{ |
| 35 |
return self::build(self::PRODUCT_STATUS, $vendorId); |
| 36 |
} |
| 37 |
|
| 38 |
public static function basalamVariationId(): string |
| 39 |
{ |
| 40 |
return self::VARIATION_ID; |
| 41 |
} |
| 42 |
|
| 43 |
public static function basalamProductMetaKeys($vendorId = null): array |
| 44 |
{ |
| 45 |
return [ |
| 46 |
self::basalamProductId($vendorId), |
| 47 |
self::basalamProductSyncStatus($vendorId), |
| 48 |
self::basalamProductStatus($vendorId), |
| 49 |
]; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* پیشوند کلیدهای اتصال؛ برای پیدا کردن کلیدهای ه� |
| 54 |
ه غرفهها (کلیدهای دارای پسوند شناسه غرفه). |
| 55 |
*/ |
| 56 |
public static function basalamProductMetaKeyPrefixes(): array |
| 57 |
{ |
| 58 |
return [ |
| 59 |
self::PRODUCT_ID, |
| 60 |
self::PRODUCT_SYNC_STATUS, |
| 61 |
self::PRODUCT_STATUS, |
| 62 |
]; |
| 63 |
} |
| 64 |
|
| 65 |
private static function build(string $baseKey, $vendorId = null): string |
| 66 |
{ |
| 67 |
$resolvedVendorId = self::normalizeVendorId($vendorId ?? self::getVendorIdFromSettings()); |
| 68 |
|
| 69 |
if ($resolvedVendorId === '') { |
| 70 |
return $baseKey; |
| 71 |
} |
| 72 |
|
| 73 |
return "{$baseKey}_{$resolvedVendorId}"; |
| 74 |
} |
| 75 |
|
| 76 |
private static function getVendorIdFromSettings() |
| 77 |
{ |
| 78 |
return syncBasalamSettings()->getSettings(SettingsConfig::VENDOR_ID); |
| 79 |
} |
| 80 |
|
| 81 |
private static function normalizeVendorId($vendorId): string |
| 82 |
{ |
| 83 |
if ($vendorId === null || $vendorId === '') { |
| 84 |
return ''; |
| 85 |
} |
| 86 |
|
| 87 |
return preg_replace('/[^a-zA-Z0-9_-]/', '', (string) $vendorId); |
| 88 |
} |
| 89 |
} |
| 90 |
|