| 1 |
<?php |
| 2 |
|
| 3 |
namespace SureCart\BlockLibrary; |
| 4 |
|
| 5 |
/** |
| 6 |
* The quick view service. |
| 7 |
*/ |
| 8 |
class ProductQuickViewService { |
| 9 |
/** |
| 10 |
* Flag to track if template has been rendered. |
| 11 |
* |
| 12 |
* @var bool |
| 13 |
*/ |
| 14 |
private static $rendered = false; |
| 15 |
|
| 16 |
/** |
| 17 |
* Include quick view template. |
| 18 |
* This needs to run before <head> so that blocks can add scripts and styles in wp_head(). |
| 19 |
* |
| 20 |
* @return void |
| 21 |
*/ |
| 22 |
public function render() { |
| 23 |
// Only render the template once per page load. |
| 24 |
if ( self::$rendered ) { |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
// do this before the footer so we can print late styles. |
| 29 |
$quick_view_template = $this->getTemplate(); |
| 30 |
|
| 31 |
// add cart template to footer. |
| 32 |
add_action( |
| 33 |
'wp_footer', |
| 34 |
function () use ( $quick_view_template ) { |
| 35 |
echo $quick_view_template; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 36 |
} |
| 37 |
); |
| 38 |
|
| 39 |
// Mark template as rendered. |
| 40 |
self::$rendered = true; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Get the cart template. |
| 45 |
* |
| 46 |
* @return string |
| 47 |
*/ |
| 48 |
public function getTemplate() { |
| 49 |
// get cart block. |
| 50 |
$template = get_block_template( 'surecart/surecart//product-quick-view', 'wp_template_part' ); |
| 51 |
if ( ! $template || empty( $template->content ) ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
ob_start(); |
| 56 |
|
| 57 |
// Render the product quick view. |
| 58 |
echo do_blocks( $template->content ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 59 |
|
| 60 |
return trim( preg_replace( '/\s+/', ' ', ob_get_clean() ) ); |
| 61 |
} |
| 62 |
} |
| 63 |
|