| 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\Print_Job_Service; |
| 12 |
use WCPOS\WooCommercePOS\Services\Cloud_Print_Trigger_Service; |
| 13 |
use WP_Error; |
| 14 |
use WCPOS\WooCommercePOS\Services\Star_Online_Client; |
| 15 |
|
| 16 |
/** |
| 17 |
* Star_Online_Adapter class. |
| 18 |
*/ |
| 19 |
class Star_Online_Adapter implements Push_Provider_Adapter_Interface { |
| 20 |
/** |
| 21 |
* Return the Star Document Markup content type. |
| 22 |
* |
| 23 |
* @return string |
| 24 |
*/ |
| 25 |
public function content_type(): string { |
| 26 |
return 'text/vnd.star.markup'; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Resolve Star Document Markup format data. |
| 31 |
* |
| 32 |
* @param array $printer Printer configuration. |
| 33 |
* @param array $template Template configuration. |
| 34 |
* |
| 35 |
* @return array{kind:string, content_type:string} |
| 36 |
*/ |
| 37 |
public function format( array $printer, array $template ): array { |
| 38 |
if ( 'thermal' !== (string) ( $template['engine'] ?? '' ) ) { |
| 39 |
return array( |
| 40 |
'kind' => '', |
| 41 |
'content_type' => '', |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
return array( |
| 46 |
'kind' => 'star-markup', |
| 47 |
'content_type' => $this->content_type(), |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Build a Star Document Markup diagnostic. |
| 53 |
* |
| 54 |
* @param string $printer_name Printer display name. |
| 55 |
* |
| 56 |
* @return array{content_type:string, payload:string} |
| 57 |
*/ |
| 58 |
public function diagnostic( string $printer_name ): array { |
| 59 |
$name = str_replace( array( '[', ']' ), array( '[[', ']]' ), $printer_name ); |
| 60 |
$markup = '[align: middle][bold: on]WCPOS[bold: off]' . "\nCloud Print Test\n[align: left]"; |
| 61 |
$markup .= 'Printer: ' . $name . "\nDate: " . gmdate( 'Y-m-d H:i' ) . "\n"; |
| 62 |
// Three feeds before the cut, matching the receipt templates' bottom margin. |
| 63 |
$markup .= "If you can read this, printing works!\n[feed][feed][feed][cut]"; |
| 64 |
|
| 65 |
return array( |
| 66 |
'content_type' => $this->content_type(), |
| 67 |
'payload' => base64_encode( $markup ), |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Resolve Star Online live status with the existing transient cache contract. |
| 73 |
* |
| 74 |
* @param array $printer Printer configuration. |
| 75 |
* @param array $context Runtime status context. |
| 76 |
* |
| 77 |
* @return string |
| 78 |
*/ |
| 79 |
public function status( array $printer, array $context ): string { |
| 80 |
$key = 'wcpos_cloud_print_star_status_' . md5( (string) $printer['id'] ); |
| 81 |
$cached = get_transient( $key ); |
| 82 |
if ( false !== $cached ) { |
| 83 |
return (string) $cached; |
| 84 |
} |
| 85 |
|
| 86 |
$api_key = (string) ( $printer['star_api_key'] ?? '' ); |
| 87 |
$url = (string) ( $printer['star_cloudprnt_url'] ?? '' ); |
| 88 |
$device_id = (string) ( $printer['star_device_id'] ?? '' ); |
| 89 |
$api_base = Star_Online_Client::api_base_from_cloudprnt_url( $url ); |
| 90 |
$group = Star_Online_Client::group_from_cloudprnt_url( $url ); |
| 91 |
$status = '' === $api_key || null === $api_base || '' === $group || '' === $device_id |
| 92 |
? 'unknown' |
| 93 |
: ( new Star_Online_Client( $api_base, $api_key ) )->device_state( $group, $device_id ); |
| 94 |
set_transient( $key, $status, (int) $context['cache_ttl'] ); |
| 95 |
|
| 96 |
return $status; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Submit a Star Online job. |
| 101 |
* |
| 102 |
* @param array $printer Printer configuration. |
| 103 |
* @param array $job Print job data. |
| 104 |
* @param string $payload Rendered payload bytes. |
| 105 |
* @param string $title Provider job title. |
| 106 |
* |
| 107 |
* @return array{success:bool, retryable:bool, error:string, external_job_id:string, drawer_error:string} |
| 108 |
*/ |
| 109 |
public function submit( array $printer, array $job, string $payload, string $title ): array { |
| 110 |
$api_key = (string) ( $printer['star_api_key'] ?? '' ); |
| 111 |
$url = (string) ( $printer['star_cloudprnt_url'] ?? '' ); |
| 112 |
$device_id = (string) ( $printer['star_device_id'] ?? '' ); |
| 113 |
$api_base = Star_Online_Client::api_base_from_cloudprnt_url( $url ); |
| 114 |
$group = Star_Online_Client::group_from_cloudprnt_url( $url ); |
| 115 |
if ( '' === $api_key || null === $api_base || '' === $group || '' === $device_id ) { |
| 116 |
return $this->failure( 'Cloud print: Star Online printer is misconfigured.', false ); |
| 117 |
} |
| 118 |
if ( '' === $payload ) { |
| 119 |
return $this->failure( 'Cloud print: Star Online job produced no printable content.', false ); |
| 120 |
} |
| 121 |
|
| 122 |
$result = ( new Star_Online_Client( $api_base, $api_key ) )->submit_job( |
| 123 |
$group, |
| 124 |
$device_id, |
| 125 |
$title, |
| 126 |
$this->content_type(), |
| 127 |
$payload |
| 128 |
); |
| 129 |
if ( is_wp_error( $result ) ) { |
| 130 |
return $this->failure( $result->get_error_message(), true ); |
| 131 |
} |
| 132 |
|
| 133 |
return array( |
| 134 |
'success' => true, |
| 135 |
'retryable' => false, |
| 136 |
'error' => '', |
| 137 |
'external_job_id' => (string) $result['id'], |
| 138 |
'drawer_error' => '', |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Build the stable submit failure shape. |
| 144 |
* |
| 145 |
* @param string $error Provider error message. |
| 146 |
* @param bool $retryable Whether the service should retry. |
| 147 |
* |
| 148 |
* @return array{success:bool, retryable:bool, error:string, external_job_id:string, drawer_error:string} |
| 149 |
*/ |
| 150 |
private function failure( string $error, bool $retryable ): array { |
| 151 |
return array( |
| 152 |
'success' => false, |
| 153 |
'retryable' => $retryable, |
| 154 |
'error' => $error, |
| 155 |
'external_job_id' => '', |
| 156 |
'drawer_error' => '', |
| 157 |
); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Queue a Star Markup test receipt and submit it through the push pipeline. |
| 162 |
* |
| 163 |
* @param array $printer Registered star-online printer. |
| 164 |
* |
| 165 |
* @return \WP_REST_Response|WP_Error |
| 166 |
*/ |
| 167 |
public function test_print( array $printer ) { |
| 168 |
$jobs = new Print_Job_Service(); |
| 169 |
$diag = $this->diagnostic( (string) $printer['name'] ); |
| 170 |
|
| 171 |
$id = $jobs->create( |
| 172 |
array( |
| 173 |
'printer_id' => $printer['id'], |
| 174 |
'content_type' => 'text/vnd.star.markup', |
| 175 |
'payload' => $diag['payload'], |
| 176 |
) |
| 177 |
); |
| 178 |
if ( $id <= 0 ) { |
| 179 |
return new WP_Error( |
| 180 |
'wcpos_print_job_create_failed', |
| 181 |
__( 'Print job could not be created.', 'woocommerce-pos' ), |
| 182 |
array( 'status' => 500 ) |
| 183 |
); |
| 184 |
} |
| 185 |
|
| 186 |
wp_schedule_single_event( time(), Cloud_Print_Trigger_Service::CRON_SUBMIT, array( $id ) ); |
| 187 |
( new \WCPOS\WooCommercePOS\Services\Cloud_Print_Submit_Service() )->submit( $id ); |
| 188 |
|
| 189 |
$response = rest_ensure_response( $jobs->get( $id ) ); |
| 190 |
$response->set_status( 201 ); |
| 191 |
|
| 192 |
return $response; |
| 193 |
} |
| 194 |
} |
| 195 |
|