' . esc_html($notice['text']) . '

'; delete_option('abankpay_admin_notice'); } } /** * Register and enqueue admin assets (only on shop_order edit screen and only for ABank orders) */ add_action('admin_enqueue_scripts', 'abankpay_admin_enqueue_assets'); function abankpay_admin_enqueue_assets($hook_suffix) { // Only on post edit screen if ($hook_suffix !== 'post.php') { return; } // Ensure function exists and we can get current screen / post $screen = function_exists('get_current_screen') ? get_current_screen() : null; if (! $screen || $screen->id !== 'shop_order') { return; } global $post; if (! $post || get_post_type($post) !== 'shop_order') { return; } $order = function_exists('wc_get_order') ? wc_get_order($post->ID) : null; if (! $order || $order->get_payment_method() !== 'abank_pay_gateway') { return; } // Version for cache-busting (fallback if not defined) $ver = defined('ABANK_PAY_VERSION') ? ABANK_PAY_VERSION : '2.0.0'; // Asset base URL constant defined in main plugin file: ABANK_PAY_PATH $base = defined('ABANK_PAY_PATH') ? ABANK_PAY_PATH : plugin_dir_url( dirname( __FILE__, 2 ) ) . '/'; // Register & enqueue styles wp_register_style( 'abankpay-admin-css', $base . 'assets/css/admin.css', array(), $ver ); wp_enqueue_style('abankpay-admin-css'); // Register & enqueue script wp_register_script( 'abankpay-admin-js', $base . 'assets/js/admin.js', array('jquery'), $ver, true ); wp_enqueue_script('abankpay-admin-js'); // Localize nonce and ajax_url for JS wp_localize_script('abankpay-admin-js', 'AbankPayData', array( 'ajax_url' => admin_url('admin-ajax.php'), // Use the same action string as check_ajax_referer will expect 'nonce' => wp_create_nonce('a_pay_check_status_nonce'), )); } /** * Add meta box to shop_order edit if order paid by ABank and status is appropriate */ add_action('add_meta_boxes', 'abankpay_add_meta_box'); function abankpay_add_meta_box() { $order_id = 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( isset( $_GET['post'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $order_id = absint( wp_unslash( $_GET['post'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended } elseif ( isset( $_GET['id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $order_id = absint( wp_unslash( $_GET['id'] ) ); } if ( ! $order_id ) { return; } $order = function_exists('wc_get_order') ? wc_get_order($order_id) : null; if ( ! $order ) { return; } if ( $order->get_payment_method() === 'abank_pay_gateway' && in_array($order->get_status(), array('on-hold', 'pending', 'processing'), true) ) { add_meta_box( 'abankpay_status_box', __('Оновити статус àPay', 'abank-pay'), 'abankpay_render_button', 'shop_order', 'side', 'high' ); } } /** * Render meta box content (only the button; JS handles the behavior) * * Avoid inline scripts here — all behavior is in assets/js/admin.js */ function abankpay_render_button($post) { $order_id = $post->ID; ?> ID) : null; if (! $order || $order->get_payment_method() !== 'abank_pay_gateway') { return; } // Empty container for modal (JS will populate/show/hide) echo ''; } /** * AJAX handler: check order status via gateway */ add_action('wp_ajax_abank_pay_check_status', 'abankpay_ajax_check_status'); function abankpay_ajax_check_status() { // Expecting nonce sent as _ajax_nonce (see JS/localize) check_ajax_referer('a_pay_check_status_nonce', '_ajax_nonce'); $order_id = isset($_POST['order_id']) ? intval($_POST['order_id']) : 0; if (! $order_id) { update_option('abankpay_admin_notice', array( 'type' => 'error', 'text' => 'Невірне ID замовлення', )); wp_send_json_success(); // client will reload page and show notice } if ( class_exists('ABANK_Pay_Gateway') ) { try { $gateway = new ABANK_Pay_Gateway(); // admin_check_status should create/update admin notice via update_option('abankpay_admin_notice', ...) if ( method_exists($gateway, 'admin_check_status') ) { $gateway->admin_check_status($order_id); } else { update_option('abankpay_admin_notice', array( 'type' => 'error', 'text' => 'Метод admin_check_status відсутній у gateway', )); } } catch ( Throwable $e ) { update_option('abankpay_admin_notice', array( 'type' => 'error', 'text' => 'Помилка при зверненні до шлюзу: ' . wp_kses_post( $e->getMessage() ), )); } } else { update_option('abankpay_admin_notice', array( 'type' => 'error', 'text' => 'àPay не знайдено', )); } wp_send_json_success(); } }