PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.0
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.0
1.10.20 1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 All 164 releases
woocommerce-pos / includes / Services / Providers / Star_Online_Adapter.php

Star_Online_Adapter.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.0, at includes/Services/Providers/Star_Online_Adapter.php

156 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $markup .= "If you can read this, printing works!\n[feed][cut]";
60
61 return array(
62 'content_type' => $this->content_type(),
63 'payload' => base64_encode( $markup ),
64 );
65 }
66
67 /**
68 * Resolve Star Online live status with the existing transient cache contract.
69 *
70 * @param array $printer Printer configuration.
71 * @param array $context Runtime status context.
72 *
73 * @return string
74 */
75 public function status( array $printer, array $context ): string {
76 $key = 'wcpos_cloud_print_star_status_' . md5( (string) $printer['id'] );
77 $cached = get_transient( $key );
78 if ( false !== $cached ) {
79 return (string) $cached;
80 }
81
82 $api_key = (string) ( $printer['star_api_key'] ?? '' );
83 $url = (string) ( $printer['star_cloudprnt_url'] ?? '' );
84 $device_id = (string) ( $printer['star_device_id'] ?? '' );
85 $api_base = Star_Online_Client::api_base_from_cloudprnt_url( $url );
86 $group = Star_Online_Client::group_from_cloudprnt_url( $url );
87 $status = '' === $api_key || null === $api_base || '' === $group || '' === $device_id
88 ? 'unknown'
89 : ( new Star_Online_Client( $api_base, $api_key ) )->device_state( $group, $device_id );
90 set_transient( $key, $status, (int) $context['cache_ttl'] );
91
92 return $status;
93 }
94
95 /**
96 * Submit a Star Online job.
97 *
98 * @param array $printer Printer configuration.
99 * @param array $job Print job data.
100 * @param string $payload Rendered payload bytes.
101 * @param string $title Provider job title.
102 *
103 * @return array{success:bool, retryable:bool, error:string, external_job_id:string, drawer_error:string}
104 */
105 public function submit( array $printer, array $job, string $payload, string $title ): array {
106 $api_key = (string) ( $printer['star_api_key'] ?? '' );
107 $url = (string) ( $printer['star_cloudprnt_url'] ?? '' );
108 $device_id = (string) ( $printer['star_device_id'] ?? '' );
109 $api_base = Star_Online_Client::api_base_from_cloudprnt_url( $url );
110 $group = Star_Online_Client::group_from_cloudprnt_url( $url );
111 if ( '' === $api_key || null === $api_base || '' === $group || '' === $device_id ) {
112 return $this->failure( 'Cloud print: Star Online printer is misconfigured.', false );
113 }
114 if ( '' === $payload ) {
115 return $this->failure( 'Cloud print: Star Online job produced no printable content.', false );
116 }
117
118 $result = ( new Star_Online_Client( $api_base, $api_key ) )->submit_job(
119 $group,
120 $device_id,
121 $title,
122 $this->content_type(),
123 $payload
124 );
125 if ( is_wp_error( $result ) ) {
126 return $this->failure( $result->get_error_message(), true );
127 }
128
129 return array(
130 'success' => true,
131 'retryable' => false,
132 'error' => '',
133 'external_job_id' => (string) $result['id'],
134 'drawer_error' => '',
135 );
136 }
137
138 /**
139 * Build the stable submit failure shape.
140 *
141 * @param string $error Provider error message.
142 * @param bool $retryable Whether the service should retry.
143 *
144 * @return array{success:bool, retryable:bool, error:string, external_job_id:string, drawer_error:string}
145 */
146 private function failure( string $error, bool $retryable ): array {
147 return array(
148 'success' => false,
149 'retryable' => $retryable,
150 'error' => $error,
151 'external_job_id' => '',
152 'drawer_error' => '',
153 );
154 }
155 }
156