| 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 |
|