| 1 |
<?php |
| 2 |
|
| 3 |
namespace SyncBasalam\Actions; |
| 4 |
|
| 5 |
defined('ABSPATH') || exit; |
| 6 |
|
| 7 |
class ActionHandler |
| 8 |
{ |
| 9 |
public static function postAction($actionName, $className) |
| 10 |
{ |
| 11 |
add_action('admin_post_' . $actionName, function () use ($actionName, $className) { |
| 12 |
$nonce = isset($_POST['_wpnonce']) ? sanitize_text_field(wp_unslash($_POST['_wpnonce'])) : ''; |
| 13 |
|
| 14 |
if (!wp_verify_nonce($nonce, $actionName . '_nonce')) { |
| 15 |
wp_die('دسترسی غیر� |
| 16 |
جاز!'); |
| 17 |
} |
| 18 |
$handler = syncBasalamContainer()->get($className); |
| 19 |
|
| 20 |
do_action('before_' . $actionName, $_POST); |
| 21 |
|
| 22 |
$result = $handler(); |
| 23 |
|
| 24 |
do_action('after_' . $actionName, $result, $_POST); |
| 25 |
$redirectTo = isset($_POST['redirect_to']) ? esc_url_raw($_POST['redirect_to']) : wp_get_referer(); |
| 26 |
wp_redirect($redirectTo ?: admin_url()); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- Intentional external/user-provided redirect. |
| 27 |
exit; |
| 28 |
}); |
| 29 |
} |
| 30 |
|
| 31 |
public static function postAjax($actionName, $className) |
| 32 |
{ |
| 33 |
add_action('wp_ajax_' . $actionName, function () use ($actionName, $className) { |
| 34 |
$nonce = isset($_POST['_wpnonce']) ? sanitize_text_field(wp_unslash($_POST['_wpnonce'])) : ''; |
| 35 |
|
| 36 |
if (!wp_verify_nonce($nonce, $actionName . '_nonce')) { |
| 37 |
wp_send_json_error([ |
| 38 |
'message' => 'دسترسی غیر� |
| 39 |
جاز!', |
| 40 |
]); |
| 41 |
} |
| 42 |
|
| 43 |
if (!current_user_can('manage_woocommerce')) { |
| 44 |
wp_send_json_error([ |
| 45 |
'message' => 'ش� |
| 46 |
ا � |
| 47 |
جاز به انجا� |
| 48 |
این ع� |
| 49 |
لیات نیستید.', |
| 50 |
], 403); |
| 51 |
} |
| 52 |
|
| 53 |
try { |
| 54 |
$handler = syncBasalamContainer()->get($className); |
| 55 |
|
| 56 |
do_action('before_' . $actionName, $_POST); |
| 57 |
|
| 58 |
$result = $handler(); |
| 59 |
|
| 60 |
do_action('after_' . $actionName, $result, $_POST); |
| 61 |
} catch (\Exception $e) { |
| 62 |
wp_send_json_error([ |
| 63 |
'message' => 'خطا در پردازش درخواست: ' . $e->getMessage(), |
| 64 |
]); |
| 65 |
} |
| 66 |
}); |
| 67 |
} |
| 68 |
|
| 69 |
} |
| 70 |
|