← All changes
|
includes/Services/Settings/Cloud_Print_Section.php
+64
-0
1.10.1
→
1.10.21
View file →
| @@ -9,8 +9,9 @@ | ||
| 9 | 9 | |
| 10 | 10 | use WCPOS\WooCommercePOS\Services\Cloud_Print_Registry; |
| 11 | 11 | use WCPOS\WooCommercePOS\Services\Cloud_Print_Relay_Service; |
| 12 | 12 | use WCPOS\WooCommercePOS\Services\Cloud_Print_Trigger_Service; |
| 13 | +use WCPOS\WooCommercePOS\Services\Print_Job_Service; | |
| 13 | 14 | use WCPOS\WooCommercePOS\Services\Provider; |
| 14 | 15 | use WCPOS\WooCommercePOS\Services\Star_Online_Client; |
| 15 | 16 | use WP_Error; |
| 16 | 17 | |
| @@ -225,8 +226,11 @@ | ||
| 225 | 226 | $clean = array( |
| 226 | 227 | 'printers' => $clean_printers, |
| 227 | 228 | 'assignments' => array_map( array( $this, 'sanitize_assignment' ), $assigns ), |
| 228 | 229 | ); |
| 230 | + | |
| 231 | + $clean['assignments'] = $this->clear_unrenderable_templates( $clean['assignments'], $clean_printers ); | |
| 232 | + | |
| 229 | 233 | update_option( $this->option_name(), $clean ); |
| 230 | 234 | |
| 231 | 235 | // Drop per-printer runtime state for printers that were removed, so a |
| 232 | 236 | // reused id cannot inherit a deleted printer's status or capabilities. |
| @@ -348,8 +352,68 @@ | ||
| 348 | 352 | $printer['autoCut'] = array_key_exists( 'autoCut', $printer ) ? rest_sanitize_boolean( $printer['autoCut'] ) : true; |
| 349 | 353 | $printer['fullReceiptRaster'] = array_key_exists( 'fullReceiptRaster', $printer ) ? rest_sanitize_boolean( $printer['fullReceiptRaster'] ) : false; |
| 350 | 354 | |
| 351 | 355 | return $printer; |
| 356 | + } | |
| 357 | + | |
| 358 | + /** | |
| 359 | + * Blank any assignment whose template its printer cannot render. | |
| 360 | + * | |
| 361 | + * A provider that declares a single template engine (Epson SDP and the Star | |
| 362 | + * providers all speak only 'thermal') renders nothing for any other engine, | |
| 363 | + * so the pairing has to be dealt with here rather than discovered as a | |
| 364 | + * receipt that never prints. | |
| 365 | + * | |
| 366 | + * Refusing the whole save was worse than the bug: one unrenderable row — | |
| 367 | + * including one the admin cannot see, because the template picker filters | |
| 368 | + * by engine and so cannot display the stored value — blocked every | |
| 369 | + * subsequent settings write and silently reverted the screen. Clearing the | |
| 370 | + * template instead always saves, and leaves the rule visibly incomplete: | |
| 371 | + * Cloud_Print_Trigger_Service skips assignments with no template, so the | |
| 372 | + * rule stops firing rather than queuing jobs that print nothing. | |
| 373 | + * | |
| 374 | + * Only resolvable templates are judged. A template_id that is empty or no | |
| 375 | + * longer exists cannot be classified and is left alone. | |
| 376 | + * | |
| 377 | + * @param array $assignments Sanitized assignments. | |
| 378 | + * @param array $printers Sanitized printers being written. | |
| 379 | + * | |
| 380 | + * @return array Assignments with unrenderable templates cleared. | |
| 381 | + */ | |
| 382 | + private function clear_unrenderable_templates( array $assignments, array $printers ): array { | |
| 383 | + $providers = array(); | |
| 384 | + foreach ( $printers as $printer ) { | |
| 385 | + if ( ! empty( $printer['id'] ) ) { | |
| 386 | + $providers[ $printer['id'] ] = (string) ( $printer['provider'] ?? '' ); | |
| 387 | + } | |
| 388 | + } | |
| 389 | + | |
| 390 | + return array_map( | |
| 391 | + function ( array $assignment ) use ( $providers ): array { | |
| 392 | + $printer_id = (string) ( $assignment['printer_id'] ?? '' ); | |
| 393 | + $template_id = (string) ( $assignment['template_id'] ?? '' ); | |
| 394 | + if ( '' === $template_id || ! isset( $providers[ $printer_id ] ) ) { | |
| 395 | + return $assignment; | |
| 396 | + } | |
| 397 | + | |
| 398 | + $supported = Provider::template_engines( $providers[ $printer_id ] ); | |
| 399 | + if ( 'all' === $supported ) { | |
| 400 | + return $assignment; | |
| 401 | + } | |
| 402 | + | |
| 403 | + $template = Print_Job_Service::load_template( $template_id ); | |
| 404 | + if ( null === $template ) { | |
| 405 | + return $assignment; | |
| 406 | + } | |
| 407 | + | |
| 408 | + if ( (string) ( $template['engine'] ?? '' ) !== $supported ) { | |
| 409 | + $assignment['template_id'] = ''; | |
| 410 | + } | |
| 411 | + | |
| 412 | + return $assignment; | |
| 413 | + }, | |
| 414 | + $assignments | |
| 415 | + ); | |
| 352 | 416 | } |
| 353 | 417 | |
| 354 | 418 | /** |
| 355 | 419 | * Sanitize a cloud assignment entry. |