| 1 |
<?php |
| 2 |
/** |
| 3 |
* BotWriter Service Response Handler |
| 4 |
* |
| 5 |
* The remote server (api.wpbotwriter.com) owns all service-level decisions. |
| 6 |
* It returns structured errors with: |
| 7 |
* - error (string) Machine-readable code for logging |
| 8 |
* - error_message (string) Human-readable text (may contain safe HTML links) |
| 9 |
* - error_level (int) 0 = log only, 1 = show admin notice |
| 10 |
* - terminal (bool) If true, the plugin should stop retrying |
| 11 |
* |
| 12 |
* The plugin shows error_message in logs as-is. |
| 13 |
* When error_level = 1, the plugin also creates a dismissible admin notice. |
| 14 |
* |
| 15 |
* @package BotWriter |
| 16 |
* @since 3.1.0 |
| 17 |
*/ |
| 18 |
|
| 19 |
if ( ! defined( 'ABSPATH' ) ) { |
| 20 |
exit; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Handle a service error during task processing. |
| 25 |
* |
| 26 |
* If error_level is 1, creates an admin notice with the human-readable message. |
| 27 |
* If terminal is true, marks retries exhausted. |
| 28 |
* |
| 29 |
* @param array &$data Log/task data array (modified in place). |
| 30 |
* @param string $error_message Human-readable message from the server (may contain HTML). |
| 31 |
* @param string $phase 'phase1' or 'phase2' — for logging context. |
| 32 |
* @param int $error_level 0 = log only, 1 = admin notice. |
| 33 |
* @param bool $terminal Whether to stop retrying. |
| 34 |
*/ |
| 35 |
function botwriter_handle_service_error( &$data, $error_message, $phase = 'phase1', $error_level = 0, $terminal = true ) { |
| 36 |
|
| 37 |
// error_level 1 → create a dismissible admin notice |
| 38 |
if ( (int) $error_level === 1 && function_exists( 'botwriter_announcements_add' ) ) { |
| 39 |
botwriter_announcements_add( |
| 40 |
__( 'Service notice', 'botwriter' ), |
| 41 |
$error_message |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
if ( $terminal ) { |
| 46 |
$data['intentosfase1'] = 8; |
| 47 |
} |
| 48 |
$data['task_status'] = 'error'; |
| 49 |
$data['error'] = $error_message; |
| 50 |
|
| 51 |
botwriter_log( "Service error handled ({$phase})", [ |
| 52 |
'log_id' => $data['id'] ?? null, |
| 53 |
'task_id' => $data['id_task'] ?? null, |
| 54 |
'error_message' => $error_message, |
| 55 |
'error_level' => $error_level, |
| 56 |
] ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Translate a WooCommerce ticket-rejection reason into a user-facing message. |
| 61 |
* |
| 62 |
* The server now sends error_message alongside reason codes, so this function |
| 63 |
* is only a fallback for the rare case where error_message is not present. |
| 64 |
* |
| 65 |
* @param string $reason Reason code from the BotWriter service. |
| 66 |
* @param string $error_message Optional human-readable message from the server. |
| 67 |
* @return string User-facing message. |
| 68 |
*/ |
| 69 |
function botwriter_translate_service_reason( $reason, $error_message = '' ) { |
| 70 |
if ( ! empty( $error_message ) ) { |
| 71 |
return esc_html( $error_message ); |
| 72 |
} |
| 73 |
return __( 'The BotWriter service could not process this request.', 'botwriter' ); |
| 74 |
} |
| 75 |
|