repository = $repository; $this->order_meta = $order_meta; $this->pdf_manager = $pdf_manager; $this->adapter = $adapter; $this->options = $options; } /** * Verify current-user permissions and validate the shared AJAX nonce. */ protected function check_request() { if (!current_user_can('edit_shop_orders') && !current_user_can('manage_woocommerce')) { wp_send_json_error(array('message' => __('Permission denied.', '01generator-shipping-integration-for-acs')), 403); } check_ajax_referer('woo_zo_myacs_lite_nonce', 'nonce'); } /** * Persist inline order option changes from the metabox. */ public function save_options() { $this->check_request(); $order_id = absint($_POST['order_id'] ?? 0); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- check_request() verified the nonce and the capability $field = sanitize_key($_POST['field'] ?? ''); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- check_request() verified the nonce and the capability $raw_value = wp_unslash($_POST['value'] ?? ''); // phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- nonce + capability verified in check_request(); sanitised on the next line $value = 'comment' === $field ? sanitize_textarea_field($raw_value) : sanitize_text_field($raw_value); $this->repository->ensure_order_row($order_id); $saved = $this->repository->save_order_options($order_id, array($field => $value)); if (!$saved) { wp_send_json_error(array('message' => __('Nothing was saved.', '01generator-shipping-integration-for-acs'))); } $labels = array( 'cod' => __('COD option saved.', '01generator-shipping-integration-for-acs'), 'parcels' => __('Parcels saved.', '01generator-shipping-integration-for-acs'), 'weight' => __('Weight saved.', '01generator-shipping-integration-for-acs'), 'comment' => __('Comment saved.', '01generator-shipping-integration-for-acs'), 'sat' => __('Saturday delivery option saved.', '01generator-shipping-integration-for-acs'), 'rec' => __('Reception delivery option saved.', '01generator-shipping-integration-for-acs'), 'return_voucher' => __('Return voucher option saved.', '01generator-shipping-integration-for-acs'), ); wp_send_json_success(array('message' => isset($labels[$field]) ? $labels[$field] : __('Saved.', '01generator-shipping-integration-for-acs'))); } /** * Create a shipment if needed, then generate and return the voucher PDF. */ public function create_print() { $this->check_request(); $order_id = absint($_POST['order_id'] ?? 0); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- check_request() verified the nonce and the capability $row = $this->repository->ensure_order_row($order_id); $reference = $row['reference']; $is_reprint = !empty($reference); if (empty($reference)) { $result = $this->adapter->create_shipment($order_id); if (empty($result['success'])) { wp_send_json_error(array('message' => $result['message'])); } $reference = $result['reference']; $vouchers = !empty($result['vouchers']) ? implode(' | ', $result['vouchers']) : ''; $this->repository->save_reference($order_id, $reference, $vouchers); $this->order_meta->set_tracking_code($order_id, $reference); } $printed = $this->adapter->print_voucher($reference); if (empty($printed['success'])) { wp_send_json_error(array('message' => !empty($printed['message']) ? $printed['message'] : __('The voucher PDF could not be generated.', '01generator-shipping-integration-for-acs'))); } wp_send_json_success(array( 'message' => $is_reprint ? __('Voucher reprinted.', '01generator-shipping-integration-for-acs') : __('Voucher created.', '01generator-shipping-integration-for-acs'), 'reference' => $reference, 'pdf_url' => $printed['url'], )); } /** * Cancel the stored shipment reference for the selected order. */ public function cancel() { $this->check_request(); $order_id = absint($_POST['order_id'] ?? 0); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- check_request() verified the nonce and the capability $row = $this->repository->ensure_order_row($order_id); $result = $this->adapter->cancel_shipment($row['reference']); if (empty($result['success'])) { wp_send_json_error(array('message' => $result['message'])); } $this->repository->clear_reference($order_id); $this->order_meta->clear_tracking_code($order_id); wp_send_json_success(array('message' => __('Voucher canceled.', '01generator-shipping-integration-for-acs'))); } /** * Request the latest tracking message and save it for the order. */ public function track() { $this->check_request(); $order_id = absint($_POST['order_id'] ?? 0); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- check_request() verified the nonce and the capability $row = $this->repository->ensure_order_row($order_id); $result = $this->adapter->track_shipment($row['reference']); if (empty($result['success'])) { wp_send_json_error(array('message' => $result['message'])); } $this->repository->save_tracking($order_id, $result['status'], $result['history']); $this->order_meta->set_tracking_summary($order_id, $result['status'], $result['history']); wp_send_json_success(array( 'message' => trim($result['status'] . ' - ' . $result['history']), 'status' => $result['status'], 'history' => $result['history'], )); } /** * Execute the close-day routine and store the execution timestamp. */ public function close_day() { $this->check_request(); $result = $this->adapter->close_day(); if (empty($result['success'])) { wp_send_json_error(array('message' => $result['message'])); } $this->options->set('last_close_day', current_time('mysql')); wp_send_json_success(array( 'message' => !empty($result['message']) ? $result['message'] : __('Close day completed.', '01generator-shipping-integration-for-acs'), 'document_url' => $result['url'], )); } /** * Delete all generated PDFs stored in the Lite uploads directory. */ public function clear_pdfs() { $this->check_request(); $deleted = $this->pdf_manager->clear_all(); $this->options->set('last_pdf_clear', current_time('mysql')); wp_send_json_success(array( 'message' => __('Generated PDFs were deleted.', '01generator-shipping-integration-for-acs'), 'deleted_count' => $deleted, )); } }