| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Modules\Templating\Bricks; |
| 4 |
|
| 5 |
|
| 6 |
use FluentCart\App\Hooks\Handlers\CPTHandler; |
| 7 |
use FluentCart\App\Modules\Data\ProductDataSetup; |
| 8 |
use FluentCart\App\Modules\Templating\AssetLoader; |
| 9 |
use FluentCart\App\Services\Renderer\ProductCardRender; |
| 10 |
|
| 11 |
class DynamicData |
| 12 |
{ |
| 13 |
public function register() |
| 14 |
{ |
| 15 |
add_filter('bricks/dynamic_tags_list', function ($tags) { |
| 16 |
$fctTags = $this->getTagsPairs(); |
| 17 |
foreach ($fctTags as $name => $label) { |
| 18 |
$tags[] = [ |
| 19 |
'name' => '{' . $name . '}', |
| 20 |
'label' => $label, |
| 21 |
'group' => 'Product - FluentCart', |
| 22 |
]; |
| 23 |
} |
| 24 |
return $tags; |
| 25 |
}); |
| 26 |
|
| 27 |
add_filter('bricks/dynamic_data/render_tag', [$this, 'renderValue'], 20, 3); |
| 28 |
add_filter('bricks/dynamic_data/render_content', [$this, 'renderValue'], 20, 3); |
| 29 |
add_filter('bricks/frontend/render_data', [$this, 'renderValue'], 20, 3); |
| 30 |
} |
| 31 |
|
| 32 |
public function getTagsPairs() |
| 33 |
{ |
| 34 |
return [ |
| 35 |
'fct_product_title:linked' => __('Product Title', 'fluent-cart'), |
| 36 |
'fct_product_image' => __('Product Image', 'fluent-cart'), |
| 37 |
'fct_product_excerpt' => __('Product Excerpt', 'fluent-cart'), |
| 38 |
'fct_product_price' => __('Product Price', 'fluent-cart'), |
| 39 |
'fct_product_button' => __('Product Button', 'fluent-cart'), |
| 40 |
'fct_product_view_button' => __('Product Button', 'fluent-cart'), // Legacy alias for backward compatibility |
| 41 |
]; |
| 42 |
} |
| 43 |
|
| 44 |
public function renderValue($tag, $post, $context = 'text') |
| 45 |
{ |
| 46 |
if (!is_string($tag)) { |
| 47 |
return $tag; |
| 48 |
} |
| 49 |
|
| 50 |
if (strpos($tag, 'fct_product_') === false) { |
| 51 |
return $tag; |
| 52 |
} |
| 53 |
|
| 54 |
if (!$post) { |
| 55 |
$post = BricksHelper::getFormCurrentPost(); |
| 56 |
} |
| 57 |
|
| 58 |
if (!$post || $post->post_type !== 'fluent-products') { |
| 59 |
return $tag; |
| 60 |
} |
| 61 |
|
| 62 |
$tagKey = str_replace(['{', '}'], '', $tag); |
| 63 |
|
| 64 |
// explode : |
| 65 |
$tagParts = explode(':', $tagKey); |
| 66 |
$tagName = array_shift($tagParts); |
| 67 |
|
| 68 |
switch ($tagName) { |
| 69 |
case 'fct_product_title': |
| 70 |
$productName = $post->post_title; |
| 71 |
if (in_array('linked', $tagParts)) { |
| 72 |
$productName = '<a href="' . get_permalink($post) . '" data-fct-field="product-title">' . $productName . '</a>'; |
| 73 |
} else { |
| 74 |
$productName = '<span data-fct-field="product-title">' . $productName . '</span>'; |
| 75 |
} |
| 76 |
return $productName; |
| 77 |
case 'fct_product_image': |
| 78 |
$productModel = ProductDataSetup::getProductModel($post->ID); |
| 79 |
if ($productModel) { |
| 80 |
ob_start(); |
| 81 |
(new ProductCardRender($productModel))->renderProductImage(); |
| 82 |
$content = ob_get_clean(); |
| 83 |
|
| 84 |
$badgesHtml = BricksHelper::renderProductBadges($productModel, BricksHelper::$imageBadgeSettings); |
| 85 |
|
| 86 |
return '<span data-fct-field="product-image">' . $content . $badgesHtml . '</span>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- renderProductBadges() escapes its own output |
| 87 |
} |
| 88 |
break; |
| 89 |
case 'fct_product_excerpt': |
| 90 |
$excerpt = $post->post_excerpt; |
| 91 |
if ($excerpt) { |
| 92 |
return '<span data-fct-field="product-excerpt">' . wp_kses_post($excerpt) . '</span>'; |
| 93 |
} |
| 94 |
return ''; |
| 95 |
case 'fct_product_price': |
| 96 |
$productModel = ProductDataSetup::getProductModel($post->ID); |
| 97 |
if ($productModel) { |
| 98 |
// Price markup relies on the .fct-sr-only rule shipped with the |
| 99 |
// single product styles; without it the labels render visibly. |
| 100 |
AssetLoader::loadSingleProductAssets(); |
| 101 |
ob_start(); |
| 102 |
(new ProductCardRender($productModel))->renderPrices(); |
| 103 |
$content = ob_get_clean(); |
| 104 |
return '<span data-fct-field="product-price">' . $content . '</span>'; |
| 105 |
} |
| 106 |
break; |
| 107 |
case 'fct_product_view_button': // Legacy alias for backward compatibility |
| 108 |
case 'fct_product_button': |
| 109 |
$productModel = ProductDataSetup::getProductModel($post->ID); |
| 110 |
if ($productModel) { |
| 111 |
ob_start(); |
| 112 |
(new ProductCardRender($productModel))->showBuyButton(); |
| 113 |
$content = ob_get_clean(); |
| 114 |
return '<span data-fct-field="product-button">' . $content . '</span>'; |
| 115 |
} |
| 116 |
break; |
| 117 |
} |
| 118 |
|
| 119 |
return $tag; |
| 120 |
} |
| 121 |
} |
| 122 |
|