PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.9.16
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.9.16
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 1.9.14 All 163 releases
woocommerce-pos / includes / Services / Cloud_Print_Diagnostic.php

Cloud_Print_Diagnostic.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.9.16, at includes/Services/Cloud_Print_Diagnostic.php

151 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Builds cloud-print test/diagnostic payloads per provider.
4 *
5 * @package WCPOS\WooCommercePOS\Services
6 */
7
8 namespace WCPOS\WooCommercePOS\Services;
9
10 /**
11 * Cloud_Print_Diagnostic class.
12 */
13 class Cloud_Print_Diagnostic {
14 /**
15 * Build a base64 diagnostic payload + content type for a provider.
16 *
17 * @param string $provider Provider key.
18 * @param string $printer_name Display name.
19 *
20 * @return array{content_type:string, payload:string}
21 *
22 * @throws \RuntimeException When the provider has no server-side diagnostic (PrintNode: see Phase 4).
23 */
24 public function build( string $provider, string $printer_name ): array {
25 if ( ! Provider::supports_server_diagnostic( $provider ) ) {
26 throw new \RuntimeException( esc_html( 'No server-side diagnostic for provider: ' . $provider ) );
27 }
28
29 // The name is concatenated into raw command streams; strip control
30 // bytes (ESC/GS/BEL…) so it cannot inject printer commands, matching
31 // the template pipeline's control-character stripping.
32 $printer_name = (string) preg_replace( '/[\x00-\x1F\x7F]/', '', $printer_name );
33
34 $date = gmdate( 'Y-m-d H:i' );
35 if ( 'epson-sdp' === $provider ) {
36 $payload = $this->epos( $printer_name, $date );
37 } elseif ( 'star-cloudprnt' === $provider ) {
38 $payload = $this->starprnt( $printer_name, $date );
39 } else {
40 $payload = $this->escpos( $printer_name, $date );
41 }
42
43 return array(
44 'content_type' => Provider::content_type( $provider ),
45 'payload' => base64_encode( $payload ),
46 );
47 }
48
49 /**
50 * Build a PrintNode diagnostic receipt as PDF bytes.
51 *
52 * A simple, black-and-white, printer-friendly page used to confirm a PrintNode
53 * printer is wired up correctly. Rendered via the shared PDF renderer.
54 *
55 * @param string $printer_name Display name.
56 *
57 * @return string PDF document bytes (begins with '%PDF-').
58 */
59 public function build_pdf( string $printer_name ): string {
60 $date = gmdate( 'Y-m-d H:i' );
61
62 $html = '<!DOCTYPE html><html><head><meta charset="utf-8">';
63 $html .= '<style>'
64 . 'body{font-family:"dejavu sans",sans-serif;color:#000;background:#fff;margin:24px;}'
65 . 'h1{font-size:18px;margin:0 0 12px;}'
66 . '.row{font-size:13px;margin:4px 0;}'
67 . '.label{font-weight:bold;}'
68 . 'hr{border:none;border-top:1px solid #000;margin:16px 0;}'
69 . '.ok{font-size:14px;font-weight:bold;margin-top:16px;}'
70 . '</style></head><body>';
71 $html .= '<h1>WCPOS &mdash; Cloud Print Test</h1>';
72 $html .= '<div class="row"><span class="label">Printer:</span> ' . esc_html( $printer_name ) . '</div>';
73 $html .= '<div class="row"><span class="label">Date (UTC):</span> ' . esc_html( $date ) . '</div>';
74 $html .= '<hr>';
75 $html .= '<div class="ok">If you can read this, printing works!</div>';
76 $html .= '</body></html>';
77
78 return ( new Pdf_Renderer() )->render_html( $html );
79 }
80
81 /**
82 * Minimal ESC/POS capability check.
83 *
84 * @param string $name Printer display name.
85 * @param string $date Render date.
86 *
87 * @return string
88 */
89 private function escpos( string $name, string $date ): string {
90 $esc = "\x1B@"; // init.
91 $esc .= "\x1Ba\x01"; // center.
92 $esc .= "WCPOS\n";
93 $esc .= "Cloud Print Test\n";
94 $esc .= "\x1Ba\x00"; // left.
95 $esc .= 'Printer: ' . $name . "\n";
96 $esc .= 'Date: ' . $date . "\n";
97 $esc .= "If you can read this, printing works!\n\n\n";
98 $esc .= "\x1DV\x41\x00"; // full cut.
99
100 return $esc;
101 }
102
103 /**
104 * Minimal native StarPRNT capability check.
105 *
106 * StarPRNT-native printers (the whole TSP100 line) cannot decode ESC/POS,
107 * so the Star diagnostic must carry StarPRNT commands. No initialize
108 * command: CloudPRNT jobs must not reset the printer; the job opens with
109 * the UTF-8 encoding select sequence instead.
110 *
111 * @param string $name Printer display name.
112 * @param string $date Render date.
113 *
114 * @return string
115 */
116 private function starprnt( string $name, string $date ): string {
117 $star = "\x1B\x1D\x29\x55\x02\x00\x30\x01"; // Select UTF-8 encoding.
118 $star .= "\x1B\x1D\x29\x55\x02\x00\x40\x00"; // UTF-8 font setting.
119 $star .= "\x1B\x1D\x61\x01"; // Center align.
120 $star .= "WCPOS\n";
121 $star .= "Cloud Print Test\n";
122 $star .= "\x1B\x1D\x61\x00"; // Left align.
123 $star .= 'Printer: ' . $name . "\n";
124 $star .= 'Date: ' . $date . "\n";
125 $star .= "If you can read this, printing works!\n\n\n";
126 $star .= "\x1B\x64\x03"; // Feed and partial cut.
127
128 return $star;
129 }
130
131 /**
132 * Minimal ePOS-Print XML capability check.
133 *
134 * @param string $name Printer display name.
135 * @param string $date Render date.
136 *
137 * @return string
138 */
139 private function epos( string $name, string $date ): string {
140 $text = "WCPOS - Cloud Print Test\n";
141 $text .= 'Printer: ' . $name . "\n";
142 $text .= 'Date: ' . $date . "\n";
143 $text .= "If you can read this, printing works!\n";
144
145 return '<epos-print xmlns="http://www.epson-pos.com/schemas/2011/03/epos-print">'
146 . '<text>' . esc_html( $text ) . '</text>'
147 . '<cut type="feed"/>'
148 . '</epos-print>';
149 }
150 }
151