PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.5
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.5
1.6.6 1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 All 49 releases
← All changes | app/Modules/Templating/Bricks/BricksLoader.php +119 -0 1.4.2 → 1.6.5 View file →
@@ -7,8 +7,10 @@
7 7 use FluentCart\App\Models\Product;
8 8
9 9 class BricksLoader
10 10 {
11 + const TEMPLATE_TYPE_PRODUCT = 'fct_product';
12 +
11 13 public function register()
12 14 {
13 15 if (!defined('BRICKS_VERSION')) {
14 16 return;
@@ -17,8 +19,12 @@
17 19 add_action('init', [$this, 'loadElements'], 20);
18 20
19 21 (new DynamicData())->register();
20 22
23 + add_filter('bricks/setup/control_options', [$this, 'addTemplateTypes']);
24 + add_filter('bricks/database/content_type', [$this, 'setContentType'], 10, 2);
25 + add_filter('bricks/active_templates', [$this, 'setActiveTemplates'], 10, 3);
26 +
21 27 add_action('wp_footer', [$this, 'addDynamicFieldClassesScript']);
22 28
23 29 add_filter('fluent_cart/template/disable_taxonomy_fallback', function ($result) {
24 30 $bricks_data = \Bricks\Database::get_template_data('archive');
@@ -52,8 +58,121 @@
52 58 $className = "\\FluentCart\\App\\Modules\\Templating\\Bricks\\Elements\\$elementKey";
53 59 Elements::register_element($elementFile, $elementName, $className);
54 60 }
55 61
62 + }
63 +
64 + /**
65 + * Add FluentCart template types to the Bricks template type dropdown.
66 + */
67 + public function addTemplateTypes($controlOptions)
68 + {
69 + if (!isset($controlOptions['templateTypes']) || !is_array($controlOptions['templateTypes'])) {
70 + return $controlOptions;
71 + }
72 +
73 + $templateTypes = $controlOptions['templateTypes'];
74 +
75 + if (isset($templateTypes[self::TEMPLATE_TYPE_PRODUCT])) {
76 + return $controlOptions;
77 + }
78 +
79 + $fluentCartType = [
80 + self::TEMPLATE_TYPE_PRODUCT => __('FluentCart - Product', 'fluent-cart'),
81 + ];
82 +
83 + if (array_key_exists('content', $templateTypes)) {
84 + $keys = array_keys($templateTypes);
85 + $offset = array_search('content', $keys, true) + 1;
86 +
87 + $templateTypes = array_slice($templateTypes, 0, $offset, true)
88 + + $fluentCartType
89 + + array_slice($templateTypes, $offset, null, true);
90 + } else {
91 + $templateTypes = array_merge($templateTypes, $fluentCartType);
92 + }
93 +
94 + $controlOptions['templateTypes'] = $templateTypes;
95 +
96 + return $controlOptions;
97 + }
98 +
99 + /**
100 + * Let Bricks resolve FluentCart single product templates as product templates.
101 + */
102 + public function setContentType($contentType, $postId)
103 + {
104 + if (is_search()) {
105 + return $contentType;
106 + }
107 +
108 + if (is_singular('fluent-products')) {
109 + return self::TEMPLATE_TYPE_PRODUCT;
110 + }
111 +
112 + return $contentType;
113 + }
114 +
115 + /**
116 + * Make the FluentCart product template type render on single product pages.
117 + */
118 + public function setActiveTemplates($activeTemplates, $postId, $contentType)
119 + {
120 + if ($contentType !== self::TEMPLATE_TYPE_PRODUCT || !is_singular('fluent-products')) {
121 + return $activeTemplates;
122 + }
123 +
124 + if (!empty($activeTemplates[self::TEMPLATE_TYPE_PRODUCT])) {
125 + $activeTemplates['content'] = $activeTemplates[self::TEMPLATE_TYPE_PRODUCT];
126 + return $activeTemplates;
127 + }
128 +
129 + $templateId = $this->getProductTemplateId($postId);
130 +
131 + if (!$templateId) {
132 + return $activeTemplates;
133 + }
134 +
135 + $activeTemplates['content'] = $templateId;
136 + $activeTemplates[self::TEMPLATE_TYPE_PRODUCT] = $templateId;
137 +
138 + return $activeTemplates;
139 + }
140 +
141 + protected function getProductTemplateId($postId)
142 + {
143 + if (!class_exists('\Bricks\Templates')) {
144 + return 0;
145 + }
146 +
147 + $templateIds = \Bricks\Templates::get_templates_by_type(self::TEMPLATE_TYPE_PRODUCT);
148 +
149 + if (empty($templateIds) || !is_array($templateIds)) {
150 + return 0;
151 + }
152 +
153 + $foundTemplates = [];
154 + $defaultTemplateId = 0;
155 +
156 + foreach ($templateIds as $templateId) {
157 + $conditions = \Bricks\Helpers::get_template_setting('templateConditions', $templateId);
158 +
159 + if (!$conditions) {
160 + if (!$defaultTemplateId) {
161 + $defaultTemplateId = absint($templateId);
162 + }
163 + continue;
164 + }
165 +
166 + $foundTemplates = \Bricks\Database::screen_conditions($foundTemplates, $templateId, $conditions, $postId, '');
167 + }
168 +
169 + if (!empty($foundTemplates)) {
170 + $scores = array_keys($foundTemplates);
171 + return $foundTemplates[max($scores)];
172 + }
173 +
174 + return $defaultTemplateId;
56 175 }
57 176
58 177 public function preloadProductCollectionsAjax($view, $args)
59 178 {