| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Services\Products; |
| 4 |
|
| 5 |
defined('ABSPATH') || exit; |
| 6 |
|
| 7 |
/** |
| 8 |
* گزارش اتصالهای تکراری برای ن� |
| 9 |
ایش به � |
| 10 |
دیر سایت. |
| 11 |
* |
| 12 |
* ه� |
| 13 |
اصلاح خودکار (هنگا� |
| 14 |
بروزرسانی افزونه) و ه� |
| 15 |
تشخیص لحظهای (هنگا� |
| 16 |
ه� |
| 17 |
گا� |
| 18 |
سازی) |
| 19 |
* نتیجه را اینجا ذخیره � |
| 20 |
یکنند تا در پیشخوان وردپرس دیده شود. |
| 21 |
*/ |
| 22 |
class DuplicateConnectionReport |
| 23 |
{ |
| 24 |
public const OPTION = 'sync_basalam_duplicate_connection_report'; |
| 25 |
public const DISMISS_QUERY_ARG = 'sync_basalam_dismiss_duplicate_report'; |
| 26 |
public const DISMISS_NONCE = 'sync_basalam_dismiss_duplicate_report_nonce'; |
| 27 |
|
| 28 |
private const MAX_ITEMS = 50; |
| 29 |
|
| 30 |
public static function registerHooks(): void |
| 31 |
{ |
| 32 |
\add_action('admin_init', [self::class, 'handleDismiss']); |
| 33 |
\add_action('admin_notices', [self::class, 'render']); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* ثبت یک اتصال � |
| 38 |
شترک تشخیص داده شده. |
| 39 |
* |
| 40 |
* @return bool آیا این � |
| 41 |
ورد تازه ثبت شد؟ |
| 42 |
*/ |
| 43 |
public static function recordConflict(int $productId, array $conflictingIds): bool |
| 44 |
{ |
| 45 |
$ids = array_values(array_unique(array_merge([$productId], array_map('intval', $conflictingIds)))); |
| 46 |
sort($ids); |
| 47 |
|
| 48 |
return self::add([ |
| 49 |
'type' => 'conflict', |
| 50 |
'product_ids' => $ids, |
| 51 |
'kept' => 0, |
| 52 |
'disconnected' => [], |
| 53 |
]); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* ثبت اتصالهای تکراری که به صورت خودکار اصلاح شدهاند. |
| 58 |
*/ |
| 59 |
public static function recordRepairs(array $repairs): void |
| 60 |
{ |
| 61 |
foreach ($repairs as $repair) { |
| 62 |
$ids = array_values(array_unique(array_merge([(int) $repair['kept']], array_map('intval', $repair['disconnected'])))); |
| 63 |
sort($ids); |
| 64 |
|
| 65 |
self::add([ |
| 66 |
'type' => 'repaired', |
| 67 |
'product_ids' => $ids, |
| 68 |
'kept' => (int) $repair['kept'], |
| 69 |
'disconnected' => array_map('intval', $repair['disconnected']), |
| 70 |
]); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
public static function all(): array |
| 75 |
{ |
| 76 |
$report = \get_option(self::OPTION, []); |
| 77 |
|
| 78 |
return is_array($report) ? $report : []; |
| 79 |
} |
| 80 |
|
| 81 |
public static function forget(): void |
| 82 |
{ |
| 83 |
\delete_option(self::OPTION); |
| 84 |
} |
| 85 |
|
| 86 |
public static function handleDismiss(): void |
| 87 |
{ |
| 88 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- The nonce itself is verified on the next line. |
| 89 |
if (empty($_GET[self::DISMISS_QUERY_ARG])) return; |
| 90 |
|
| 91 |
if (!\current_user_can('manage_woocommerce')) return; |
| 92 |
|
| 93 |
$nonce = isset($_GET['_wpnonce']) ? \sanitize_text_field(\wp_unslash($_GET['_wpnonce'])) : ''; |
| 94 |
|
| 95 |
if (!\wp_verify_nonce($nonce, self::DISMISS_NONCE)) return; |
| 96 |
|
| 97 |
self::forget(); |
| 98 |
|
| 99 |
\wp_safe_redirect(\remove_query_arg([self::DISMISS_QUERY_ARG, '_wpnonce'])); |
| 100 |
exit; |
| 101 |
} |
| 102 |
|
| 103 |
public static function render(): void |
| 104 |
{ |
| 105 |
if (!\current_user_can('manage_woocommerce')) return; |
| 106 |
|
| 107 |
if (!self::all()) return; |
| 108 |
|
| 109 |
require syncBasalamPlugin()->templatePath('notifications/DuplicateConnectionAlert.php'); |
| 110 |
} |
| 111 |
|
| 112 |
public static function dismissUrl(): string |
| 113 |
{ |
| 114 |
return \wp_nonce_url( |
| 115 |
\add_query_arg(self::DISMISS_QUERY_ARG, 1), |
| 116 |
self::DISMISS_NONCE |
| 117 |
); |
| 118 |
} |
| 119 |
|
| 120 |
private static function add(array $item): bool |
| 121 |
{ |
| 122 |
$report = self::all(); |
| 123 |
$key = $item['type'] . ':' . implode('-', $item['product_ids']); |
| 124 |
|
| 125 |
if (isset($report[$key])) return false; |
| 126 |
|
| 127 |
if (count($report) >= self::MAX_ITEMS) array_shift($report); |
| 128 |
|
| 129 |
$item['recorded_at'] = \current_time('mysql'); |
| 130 |
$report[$key] = $item; |
| 131 |
|
| 132 |
\update_option(self::OPTION, $report, false); |
| 133 |
|
| 134 |
return true; |
| 135 |
} |
| 136 |
} |
| 137 |
|