| 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\Services\Renderer\ProductCardRender; |
| 9 |
|
| 10 |
class DynamicData |
| 11 |
{ |
| 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_price' => __('Product Price', 'fluent-cart'), |
| 38 |
'fct_product_view_button' => __('Product Link Button', 'fluent-cart'), |
| 39 |
]; |
| 40 |
} |
| 41 |
|
| 42 |
public function renderValue($tag, $post, $context = 'text') |
| 43 |
{ |
| 44 |
if (!is_string($tag)) { |
| 45 |
return $tag; |
| 46 |
} |
| 47 |
|
| 48 |
if (strpos($tag, 'fct_product_') === false) { |
| 49 |
return $tag; |
| 50 |
} |
| 51 |
|
| 52 |
if (!$post) { |
| 53 |
$post = BricksHelper::getFormCurrentPost(); |
| 54 |
} |
| 55 |
|
| 56 |
if (!$post || $post->post_type !== 'fluent-products') { |
| 57 |
return $tag; |
| 58 |
} |
| 59 |
|
| 60 |
$tagKey = str_replace(['{', '}'], '', $tag); |
| 61 |
|
| 62 |
// explode : |
| 63 |
$tagParts = explode(':', $tagKey); |
| 64 |
$tagName = array_shift($tagParts); |
| 65 |
|
| 66 |
switch ($tagName) { |
| 67 |
case 'fct_product_title': |
| 68 |
$productName = $post->post_title; |
| 69 |
if (in_array('linked', $tagParts)) { |
| 70 |
$productName = '<a href="' . get_permalink($post) . '">' . $productName . '</a>'; |
| 71 |
} |
| 72 |
return $productName; |
| 73 |
case 'fct_product_image': |
| 74 |
$productModel = ProductDataSetup::getProductModel($post->ID); |
| 75 |
if ($productModel) { |
| 76 |
ob_start(); |
| 77 |
(new ProductCardRender($productModel))->renderProductImage(); |
| 78 |
return ob_get_clean(); |
| 79 |
} |
| 80 |
break; |
| 81 |
case 'fct_product_price': |
| 82 |
$productModel = ProductDataSetup::getProductModel($post->ID); |
| 83 |
if ($productModel) { |
| 84 |
ob_start(); |
| 85 |
(new ProductCardRender($productModel))->renderPrices(); |
| 86 |
return ob_get_clean(); |
| 87 |
} |
| 88 |
break; |
| 89 |
case 'fct_product_button': |
| 90 |
$productModel = ProductDataSetup::getProductModel($post->ID); |
| 91 |
if ($productModel) { |
| 92 |
ob_start(); |
| 93 |
(new ProductCardRender($productModel))->showBuyButton(); |
| 94 |
return ob_get_clean(); |
| 95 |
} |
| 96 |
break; |
| 97 |
} |
| 98 |
|
| 99 |
return $tag; |
| 100 |
} |
| 101 |
} |
| 102 |
|