| 1 |
<?php |
| 2 |
/** |
| 3 |
* StarIO.Online provider adapter. |
| 4 |
* |
| 5 |
* @package WCPOS\WooCommercePOS\Services\Providers |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WCPOS\WooCommercePOS\Services\Providers; |
| 9 |
|
| 10 |
use WCPOS\WooCommercePOS\Interfaces\Push_Provider_Adapter_Interface; |
| 11 |
use WCPOS\WooCommercePOS\Services\Star_Online_Client; |
| 12 |
|
| 13 |
/** |
| 14 |
* Star_Online_Adapter class. |
| 15 |
*/ |
| 16 |
class Star_Online_Adapter implements Push_Provider_Adapter_Interface { |
| 17 |
/** |
| 18 |
* Return the Star Document Markup content type. |
| 19 |
* |
| 20 |
* @return string |
| 21 |
*/ |
| 22 |
public function content_type(): string { |
| 23 |
return 'text/vnd.star.markup'; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Resolve Star Document Markup format data. |
| 28 |
* |
| 29 |
* @param array $printer Printer configuration. |
| 30 |
* @param array $template Template configuration. |
| 31 |
* |
| 32 |
* @return array{kind:string, content_type:string} |
| 33 |
*/ |
| 34 |
public function format( array $printer, array $template ): array { |
| 35 |
if ( 'thermal' !== (string) ( $template['engine'] ?? '' ) ) { |
| 36 |
return array( |
| 37 |
'kind' => '', |
| 38 |
'content_type' => '', |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
return array( |
| 43 |
'kind' => 'star-markup', |
| 44 |
'content_type' => $this->content_type(), |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Build a Star Document Markup diagnostic. |
| 50 |
* |
| 51 |
* @param string $printer_name Printer display name. |
| 52 |
* |
| 53 |
* @return array{content_type:string, payload:string} |
| 54 |
*/ |
| 55 |
public function diagnostic( string $printer_name ): array { |
| 56 |
$name = str_replace( array( '[', ']' ), array( '[[', ']]' ), $printer_name ); |
| 57 |
$markup = '[align: middle][bold: on]WCPOS[bold: off]' . "\nCloud Print Test\n[align: left]"; |
| 58 |
$markup .= 'Printer: ' . $name . "\nDate: " . gmdate( 'Y-m-d H:i' ) . "\n"; |
| 59 |
// Three feeds before the cut, matching the receipt templates' bottom margin. |
| 60 |
$markup .= "If you can read this, printing works!\n[feed][feed][feed][cut]"; |
| 61 |
|
| 62 |
return array( |
| 63 |
'content_type' => $this->content_type(), |
| 64 |
'payload' => base64_encode( $markup ), |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Resolve Star Online live status with the existing transient cache contract. |
| 70 |
* |
| 71 |
* @param array $printer Printer configuration. |
| 72 |
* @param array $context Runtime status context. |
| 73 |
* |
| 74 |
* @return string |
| 75 |
*/ |
| 76 |
public function status( array $printer, array $context ): string { |
| 77 |
$key = 'wcpos_cloud_print_star_status_' . md5( (string) $printer['id'] ); |
| 78 |
$cached = get_transient( $key ); |
| 79 |
if ( false !== $cached ) { |
| 80 |
return (string) $cached; |
| 81 |
} |
| 82 |
|
| 83 |
$api_key = (string) ( $printer['star_api_key'] ?? '' ); |
| 84 |
$url = (string) ( $printer['star_cloudprnt_url'] ?? '' ); |
| 85 |
$device_id = (string) ( $printer['star_device_id'] ?? '' ); |
| 86 |
$api_base = Star_Online_Client::api_base_from_cloudprnt_url( $url ); |
| 87 |
$group = Star_Online_Client::group_from_cloudprnt_url( $url ); |
| 88 |
$status = '' === $api_key || null === $api_base || '' === $group || '' === $device_id |
| 89 |
? 'unknown' |
| 90 |
: ( new Star_Online_Client( $api_base, $api_key ) )->device_state( $group, $device_id ); |
| 91 |
set_transient( $key, $status, (int) $context['cache_ttl'] ); |
| 92 |
|
| 93 |
return $status; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Submit a Star Online job. |
| 98 |
* |
| 99 |
* @param array $printer Printer configuration. |
| 100 |
* @param array $job Print job data. |
| 101 |
* @param string $payload Rendered payload bytes. |
| 102 |
* @param string $title Provider job title. |
| 103 |
* |
| 104 |
* @return array{success:bool, retryable:bool, error:string, external_job_id:string, drawer_error:string} |
| 105 |
*/ |
| 106 |
public function submit( array $printer, array $job, string $payload, string $title ): array { |
| 107 |
$api_key = (string) ( $printer['star_api_key'] ?? '' ); |
| 108 |
$url = (string) ( $printer['star_cloudprnt_url'] ?? '' ); |
| 109 |
$device_id = (string) ( $printer['star_device_id'] ?? '' ); |
| 110 |
$api_base = Star_Online_Client::api_base_from_cloudprnt_url( $url ); |
| 111 |
$group = Star_Online_Client::group_from_cloudprnt_url( $url ); |
| 112 |
if ( '' === $api_key || null === $api_base || '' === $group || '' === $device_id ) { |
| 113 |
return $this->failure( 'Cloud print: Star Online printer is misconfigured.', false ); |
| 114 |
} |
| 115 |
if ( '' === $payload ) { |
| 116 |
return $this->failure( 'Cloud print: Star Online job produced no printable content.', false ); |
| 117 |
} |
| 118 |
|
| 119 |
$result = ( new Star_Online_Client( $api_base, $api_key ) )->submit_job( |
| 120 |
$group, |
| 121 |
$device_id, |
| 122 |
$title, |
| 123 |
$this->content_type(), |
| 124 |
$payload |
| 125 |
); |
| 126 |
if ( is_wp_error( $result ) ) { |
| 127 |
return $this->failure( $result->get_error_message(), true ); |
| 128 |
} |
| 129 |
|
| 130 |
return array( |
| 131 |
'success' => true, |
| 132 |
'retryable' => false, |
| 133 |
'error' => '', |
| 134 |
'external_job_id' => (string) $result['id'], |
| 135 |
'drawer_error' => '', |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Build the stable submit failure shape. |
| 141 |
* |
| 142 |
* @param string $error Provider error message. |
| 143 |
* @param bool $retryable Whether the service should retry. |
| 144 |
* |
| 145 |
* @return array{success:bool, retryable:bool, error:string, external_job_id:string, drawer_error:string} |
| 146 |
*/ |
| 147 |
private function failure( string $error, bool $retryable ): array { |
| 148 |
return array( |
| 149 |
'success' => false, |
| 150 |
'retryable' => $retryable, |
| 151 |
'error' => $error, |
| 152 |
'external_job_id' => '', |
| 153 |
'drawer_error' => '', |
| 154 |
); |
| 155 |
} |
| 156 |
} |
| 157 |
|