PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.2
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.2
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 / Templates / Renderers / Logicless_Renderer.php

Logicless_Renderer.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.2, at includes/Templates/Renderers/Logicless_Renderer.php

97 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Logicless receipt renderer.
4 *
5 * Uses Mustache.php to render templates with section blocks:
6 * {{#key}}...{{/key}} — iterate arrays or show block for truthy values
7 * {{^key}}...{{/key}} — show block when value is empty/falsy
8 * {{.}} — current value (for arrays of scalars)
9 * {{key.path}} — dot-path placeholder substitution
10 *
11 * Money fields are pre-formatted as currency before rendering.
12 *
13 * @package WCPOS\WooCommercePOS\Templates\Renderers
14 */
15
16 namespace WCPOS\WooCommercePOS\Templates\Renderers;
17
18 use Mustache\Engine as Mustache_Engine;
19 use WCPOS\WooCommercePOS\Interfaces\Receipt_Renderer_Interface;
20 use WCPOS\WooCommercePOS\Services\Receipt_Data_Schema;
21 use WCPOS\WooCommercePOS\Templates\Barcode_Image;
22 use WC_Abstract_Order;
23
24 /**
25 * Logicless_Renderer class.
26 */
27 class Logicless_Renderer implements Receipt_Renderer_Interface {
28
29 /**
30 * Render logicless template output.
31 *
32 * @param array $template Template metadata/content.
33 * @param WC_Abstract_Order|null $order Order object, or null for sample-data preview.
34 * @param array $receipt_data Canonical receipt payload.
35 */
36 public function render( array $template, ?WC_Abstract_Order $order, array $receipt_data ): void {
37 $content = isset( $template['content'] ) && \is_string( $template['content'] ) ? $template['content'] : '';
38
39 if ( '' === $content ) {
40 echo '<!-- Empty logicless receipt template -->';
41 return;
42 }
43
44 $currency = $receipt_data['order']['currency'] ?? 'USD';
45 $formatted_data = Receipt_Data_Schema::format_money_fields( $receipt_data, $currency );
46
47 // Safety net: if a template uses {{#t}}...{{/t}} markers (from gallery source),
48 // setting t = true makes Mustache pass the inner text through unchanged.
49 $formatted_data['t'] = true;
50
51 // Strip HTML comments — wp_kses_post removes the delimiters but leaves the text.
52 $content = preg_replace( '/<!--.*?-->/s', '', $content );
53
54 $flags = ENT_QUOTES | ENT_SUBSTITUTE;
55 $mustache = new Mustache_Engine(
56 array(
57 'entity_flags' => $flags,
58 'escape' => function ( $value ) use ( $flags ) {
59 if ( \is_array( $value ) ) {
60 return '';
61 }
62
63 return htmlspecialchars( (string) $value, $flags, 'UTF-8' );
64 },
65 )
66 );
67
68 $output = $mustache->render( $content, $formatted_data );
69
70 // Swap <barcode>/<qrcode> markup for placeholder tokens before sanitizing
71 // (wp_kses_post would otherwise strip the unknown elements, leaving only the
72 // bare value as text). The rasterized PNG <img> tags are spliced back in
73 // after sanitization, so their data: image URI never has to be whitelisted
74 // in kses. Mirrors the client-side preview renderer.
75 $barcode_images = array();
76 $output = Barcode_Image::replace_markup( $output, $barcode_images );
77
78 // Allow print-color-adjust in inline styles so background fills survive print.
79 // wp_kses_post drops CSS properties not on the safe_style_css allowlist by default.
80 $allow_print_color_adjust = function ( array $styles ): array {
81 $styles[] = 'print-color-adjust';
82 $styles[] = '-webkit-print-color-adjust';
83
84 return $styles;
85 };
86
87 add_filter( 'safe_style_css', $allow_print_color_adjust );
88
89 try {
90 $sanitized = wp_kses_post( $output );
91 echo $barcode_images ? strtr( $sanitized, $barcode_images ) : $sanitized; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $sanitized is kses'd; spliced values are self-generated PNG <img> tags.
92 } finally {
93 remove_filter( 'safe_style_css', $allow_print_color_adjust );
94 }
95 }
96 }
97