| @@ -11,8 +11,9 @@ | ||
| 11 | 11 | |
| 12 | 12 | namespace WCPOS\WooCommercePOS\Services; |
| 13 | 13 | |
| 14 | 14 | use WCPOS\WooCommercePOS\Interfaces\Provider_Adapter_Interface; |
| 15 | +use WCPOS\WooCommercePOS\Interfaces\Poll_Provider_Adapter_Interface; | |
| 15 | 16 | use WCPOS\WooCommercePOS\Services\Providers\Epson_Sdp_Adapter; |
| 16 | 17 | use WCPOS\WooCommercePOS\Services\Providers\Printnode_Adapter; |
| 17 | 18 | use WCPOS\WooCommercePOS\Services\Providers\Star_Cloudprnt_Adapter; |
| 18 | 19 | use WCPOS\WooCommercePOS\Services\Providers\Star_Online_Adapter; |
| @@ -132,8 +133,63 @@ | ||
| 132 | 133 | return new Star_Online_Adapter(); |
| 133 | 134 | default: |
| 134 | 135 | return null; |
| 135 | 136 | } |
| 137 | + } | |
| 138 | + | |
| 139 | + /** | |
| 140 | + * Resolve the wire format and HTTP content type for a print job. | |
| 141 | + * | |
| 142 | + * @param array $printer Printer configuration. | |
| 143 | + * @param array $template Template configuration. | |
| 144 | + * | |
| 145 | + * @return array{kind:string, content_type:string} | |
| 146 | + */ | |
| 147 | + public static function format( array $printer, array $template ): array { | |
| 148 | + // Printer rows saved before the provider field existed have none; they must behave as the default provider. | |
| 149 | + $provider = self::normalize( \is_string( $printer['provider'] ?? null ) ? $printer['provider'] : null ); | |
| 150 | + $adapter = self::adapter( $provider ); | |
| 151 | + if ( null === $adapter ) { | |
| 152 | + return array( | |
| 153 | + 'kind' => '', | |
| 154 | + 'content_type' => '', | |
| 155 | + ); | |
| 156 | + } | |
| 157 | + | |
| 158 | + return $adapter->format( $printer, $template ); | |
| 159 | + } | |
| 160 | + | |
| 161 | + /** | |
| 162 | + * Resolve the HTTP content type for a printer when no template is in hand. | |
| 163 | + * | |
| 164 | + * The reprint path uses this only when the template cannot be rendered and | |
| 165 | + * the job carries no `pn_kind`; otherwise it keeps the stored content type | |
| 166 | + * so the two halves cannot drift apart. | |
| 167 | + * | |
| 168 | + * PrintNode reports its PDF default even for a printer in raw mode. The | |
| 169 | + * reprint path's `pn_kind` condition keeps raw jobs away from this answer. | |
| 170 | + * Prefer format() when a template is in hand to resolve both halves together. | |
| 171 | + * | |
| 172 | + * @param array $printer Printer configuration. | |
| 173 | + * | |
| 174 | + * @return string | |
| 175 | + */ | |
| 176 | + public static function printer_content_type( array $printer ): string { | |
| 177 | + $provider = self::normalize( \is_string( $printer['provider'] ?? null ) ? $printer['provider'] : null ); | |
| 178 | + $adapter = self::adapter( $provider ); | |
| 179 | + | |
| 180 | + return null === $adapter ? 'application/octet-stream' : $adapter->content_type(); | |
| 181 | + } | |
| 182 | + | |
| 183 | + /** | |
| 184 | + * Resolve only polling adapters without widening the base provider contract. | |
| 185 | + * | |
| 186 | + * @param string $key Provider key. | |
| 187 | + * @return Poll_Provider_Adapter_Interface|null | |
| 188 | + */ | |
| 189 | + public static function poll_adapter( string $key ): ?Poll_Provider_Adapter_Interface { | |
| 190 | + $adapter = self::adapter( $key ); | |
| 191 | + return $adapter instanceof Poll_Provider_Adapter_Interface ? $adapter : null; | |
| 136 | 192 | } |
| 137 | 193 | |
| 138 | 194 | /** |
| 139 | 195 | * Whether the provider polls the server for jobs. |