| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Modules\Templating\Bricks\Elements; |
| 4 |
|
| 5 |
|
| 6 |
use Bricks\Element; |
| 7 |
use Bricks\Setup; |
| 8 |
use FluentCart\App\Modules\Data\ProductDataSetup; |
| 9 |
use FluentCart\App\Modules\Templating\AssetLoader; |
| 10 |
use FluentCart\App\Services\Renderer\ProductRenderer; |
| 11 |
use FluentCart\App\Modules\Templating\Bricks\BricksLoader; |
| 12 |
|
| 13 |
if (!defined('ABSPATH')) exit; // Exit if accessed directly |
| 14 |
|
| 15 |
class ProductGallery extends Element |
| 16 |
{ |
| 17 |
public $category = 'fluent-cart'; |
| 18 |
public $name = 'fct-product-gallery'; |
| 19 |
public $icon = 'ti-gallery fluent-cart-element-icon'; |
| 20 |
|
| 21 |
public function enqueue_scripts() |
| 22 |
{ |
| 23 |
AssetLoader::loadSingleProductAssets(); |
| 24 |
} |
| 25 |
|
| 26 |
public function get_label() |
| 27 |
{ |
| 28 |
return esc_html__('Product Gallery', 'fluent-cart'); |
| 29 |
} |
| 30 |
|
| 31 |
public function set_controls() |
| 32 |
{ |
| 33 |
$this->controls['_width']['rerender'] = true; |
| 34 |
$this->controls['_widthMin']['rerender'] = true; |
| 35 |
$this->controls['_widthMax']['rerender'] = true; |
| 36 |
|
| 37 |
$this->controls['queryType'] = [ |
| 38 |
'tab' => 'content', |
| 39 |
'type' => 'select', |
| 40 |
'label' => esc_html__('Query Type', 'fluent-cart'), |
| 41 |
'options' => [ |
| 42 |
'default' => esc_html__('Default', 'fluent-cart'), |
| 43 |
'custom' => esc_html__('Custom', 'fluent-cart'), |
| 44 |
], |
| 45 |
'default' => 'default', |
| 46 |
'inline' => true, |
| 47 |
]; |
| 48 |
|
| 49 |
$this->controls['productId'] = [ |
| 50 |
'tab' => 'content', |
| 51 |
'type' => 'select', |
| 52 |
'label' => esc_html__('Product', 'fluent-cart'), |
| 53 |
'options' => BricksLoader::getProductOptions(), |
| 54 |
'placeholder' => esc_html__('Select a product', 'fluent-cart'), |
| 55 |
'searchable' => true, |
| 56 |
'rerender' => true, |
| 57 |
'required' => ['queryType', '=', 'custom'], |
| 58 |
]; |
| 59 |
|
| 60 |
$this->controls['manualProductId'] = [ |
| 61 |
'tab' => 'content', |
| 62 |
'type' => 'text', |
| 63 |
'label' => esc_html__('Manual Product ID', 'fluent-cart'), |
| 64 |
'description' => esc_html__('Use this if the product is not available in dropdown.', 'fluent-cart'), |
| 65 |
'required' => [['queryType', '=', 'custom'], ['productId', '=', '']], |
| 66 |
]; |
| 67 |
|
| 68 |
$this->controls['productImageSize'] = [ |
| 69 |
'tab' => 'content', |
| 70 |
'label' => esc_html__('Product', 'fluent-cart') . ': ' . esc_html__('Image size', 'fluent-cart'), |
| 71 |
'type' => 'select', |
| 72 |
'options' => Setup::get_image_sizes_options(), |
| 73 |
'placeholder' => 'image_size', |
| 74 |
]; |
| 75 |
|
| 76 |
$this->controls['thumbnailImageSize'] = [ |
| 77 |
'tab' => 'content', |
| 78 |
'label' => esc_html__('Thumbnail', 'fluent-cart') . ': ' . esc_html__('Image size', 'fluent-cart'), |
| 79 |
'type' => 'select', |
| 80 |
'options' => Setup::get_image_sizes_options(), |
| 81 |
'placeholder' => 'thumbnail', |
| 82 |
]; |
| 83 |
|
| 84 |
$this->controls['lightboxImageSize'] = [ |
| 85 |
'tab' => 'content', |
| 86 |
'label' => esc_html__('Lightbox', 'fluent-cart') . ': ' . esc_html__('Image size', 'fluent-cart'), |
| 87 |
'type' => 'select', |
| 88 |
'options' => Setup::get_image_sizes_options(), |
| 89 |
'placeholder' => 'full', |
| 90 |
]; |
| 91 |
|
| 92 |
// THUMBNAILS |
| 93 |
|
| 94 |
$this->controls['thumbnailSep'] = [ |
| 95 |
'tab' => 'content', |
| 96 |
'label' => esc_html__('Thumbnail navigation', 'fluent-cart'), |
| 97 |
'type' => 'separator', |
| 98 |
]; |
| 99 |
|
| 100 |
$this->controls['thumbnailPosition'] = [ |
| 101 |
'tab' => 'content', |
| 102 |
'label' => esc_html__('Position', 'fluent-cart'), |
| 103 |
'type' => 'select', |
| 104 |
'inline' => true, |
| 105 |
'options' => [ |
| 106 |
'top' => esc_html__('Top', 'fluent-cart'), |
| 107 |
'right' => esc_html__('Right', 'fluent-cart'), |
| 108 |
'bottom' => esc_html__('Bottom', 'fluent-cart'), |
| 109 |
'left' => esc_html__('Left', 'fluent-cart'), |
| 110 |
], |
| 111 |
'placeholder' => esc_html__('Bottom', 'fluent-cart'), |
| 112 |
'rerender' => true, |
| 113 |
]; |
| 114 |
|
| 115 |
$this->controls['scrollableThumbs'] = [ |
| 116 |
'tab' => 'content', |
| 117 |
'label' => esc_html__('Scrollable thumbnails', 'fluent-cart'), |
| 118 |
'type' => 'checkbox', |
| 119 |
'inline' => true, |
| 120 |
'rerender' => true, |
| 121 |
]; |
| 122 |
|
| 123 |
$this->controls['maxThumbnails'] = [ |
| 124 |
'tab' => 'content', |
| 125 |
'label' => esc_html__('Max thumbnails', 'fluent-cart'), |
| 126 |
'type' => 'number', |
| 127 |
'min' => 1, |
| 128 |
'max' => 50, |
| 129 |
'placeholder' => esc_html__('No limit', 'fluent-cart'), |
| 130 |
'rerender' => true, |
| 131 |
]; |
| 132 |
|
| 133 |
$this->controls['itemWidth'] = [ |
| 134 |
'tab' => 'content', |
| 135 |
'label' => esc_html__('Item width', 'fluent-cart') . ' (px)', |
| 136 |
'type' => 'number', |
| 137 |
'units' => true, |
| 138 |
'css' => [ |
| 139 |
[ |
| 140 |
'selector' => '.fct-gallery-thumb-controls .fct-gallery-thumb-control-button', |
| 141 |
'property' => 'width', |
| 142 |
] |
| 143 |
], |
| 144 |
'placeholder' => '70px', |
| 145 |
'rerender' => true |
| 146 |
]; |
| 147 |
$this->controls['itemHeight'] = [ |
| 148 |
'tab' => 'content', |
| 149 |
'label' => esc_html__('Item Height', 'fluent-cart') . ' (px)', |
| 150 |
'type' => 'number', |
| 151 |
'units' => true, |
| 152 |
'css' => [ |
| 153 |
[ |
| 154 |
'selector' => '.fct-gallery-thumb-controls .fct-gallery-thumb-control-button', |
| 155 |
'property' => 'height', |
| 156 |
] |
| 157 |
], |
| 158 |
'placeholder' => '70px', |
| 159 |
'rerender' => true |
| 160 |
]; |
| 161 |
|
| 162 |
$this->controls['gap'] = [ |
| 163 |
'tab' => 'content', |
| 164 |
'label' => esc_html__('Gap', 'fluent-cart'), |
| 165 |
'type' => 'number', |
| 166 |
'units' => true, |
| 167 |
'css' => [ |
| 168 |
[ |
| 169 |
|
| 170 |
'selector' => '.fct-gallery-thumb-controls', |
| 171 |
'property' => 'gap', |
| 172 |
] |
| 173 |
], |
| 174 |
'placeholder' => '30px', |
| 175 |
]; |
| 176 |
|
| 177 |
$this->controls['thumbnailOpacity'] = [ |
| 178 |
'tab' => 'content', |
| 179 |
'label' => esc_html__('Opacity', 'fluent-cart'), |
| 180 |
'type' => 'number', |
| 181 |
'step' => 0.1, |
| 182 |
'css' => [ |
| 183 |
[ |
| 184 |
'selector' => '.fct-gallery-thumb-controls .fct-gallery-thumb-control-button:not(.active) img', |
| 185 |
'property' => 'opacity', |
| 186 |
] |
| 187 |
], |
| 188 |
'placeholder' => '0.3', |
| 189 |
]; |
| 190 |
|
| 191 |
$this->controls['thumbnailActiveOpacity'] = [ |
| 192 |
'tab' => 'content', |
| 193 |
'label' => esc_html__('Opacity', 'fluent-cart') . ' (' . esc_html__('Active', 'fluent-cart') . ')', |
| 194 |
'type' => 'number', |
| 195 |
'step' => 0.1, |
| 196 |
'css' => [ |
| 197 |
[ |
| 198 |
'selector' => '.fct-gallery-thumb-controls .fct-gallery-thumb-control-button.active', |
| 199 |
'property' => 'opacity', |
| 200 |
] |
| 201 |
], |
| 202 |
]; |
| 203 |
|
| 204 |
$this->controls['thumbnailBorder'] = [ |
| 205 |
'tab' => 'content', |
| 206 |
'label' => esc_html__('Border', 'fluent-cart'), |
| 207 |
'type' => 'border', |
| 208 |
'css' => [ |
| 209 |
[ |
| 210 |
'selector' => '.fct-gallery-thumb-controls .fct-gallery-thumb-control-button', |
| 211 |
'property' => 'border', |
| 212 |
] |
| 213 |
], |
| 214 |
]; |
| 215 |
|
| 216 |
$this->controls['thumbnailActiveBorder'] = [ |
| 217 |
'tab' => 'content', |
| 218 |
'label' => esc_html__('Border', 'fluent-cart') . ' (' . esc_html__('Active', 'fluent-cart') . ')', |
| 219 |
'type' => 'border', |
| 220 |
'css' => [ |
| 221 |
[ |
| 222 |
'selector' => '.fct-gallery-thumb-controls .fct-gallery-thumb-control-button.active', |
| 223 |
'property' => 'border', |
| 224 |
] |
| 225 |
], |
| 226 |
]; |
| 227 |
} |
| 228 |
|
| 229 |
public function render() |
| 230 |
{ |
| 231 |
$settings = $this->settings; |
| 232 |
$queryType = $settings['queryType'] ?? 'default'; |
| 233 |
|
| 234 |
if ($queryType === 'default') { |
| 235 |
$productId = $this->post_id; |
| 236 |
} else { |
| 237 |
$productId = !empty($settings['productId']) ? \intval($settings['productId']) : 0; |
| 238 |
$manualProductId = !empty($settings['manualProductId']) ? \intval($settings['manualProductId']) : 0; |
| 239 |
|
| 240 |
if (!$productId && $manualProductId) { |
| 241 |
$productId = $manualProductId; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
$product = ProductDataSetup::getProductModel($productId); |
| 246 |
|
| 247 |
if (!$product) { |
| 248 |
return $this->render_element_placeholder([ |
| 249 |
'title' => esc_html__( |
| 250 |
'Select a product', |
| 251 |
'fluent-cart' |
| 252 |
), |
| 253 |
]); |
| 254 |
} |
| 255 |
|
| 256 |
// Thumbnail position |
| 257 |
$thumbnail_position = !empty($settings['thumbnailPosition']) ? $settings['thumbnailPosition'] : 'bottom'; |
| 258 |
if (!in_array($thumbnail_position, ['bottom', 'top', 'left', 'right'], true)) { |
| 259 |
$thumbnail_position = 'bottom'; |
| 260 |
} |
| 261 |
$this->set_attribute('_root', 'data-pos', esc_attr($thumbnail_position)); |
| 262 |
|
| 263 |
$scrollable_thumbs = !empty($settings['scrollableThumbs']) ? 'yes' : 'no'; |
| 264 |
|
| 265 |
$max_thumbnails = !empty($settings['maxThumbnails']) ? (int) $settings['maxThumbnails'] : null; |
| 266 |
if ($max_thumbnails !== null && $max_thumbnails <= 0) { |
| 267 |
$max_thumbnails = null; |
| 268 |
} |
| 269 |
|
| 270 |
?> |
| 271 |
|
| 272 |
<div |
| 273 |
<?php |
| 274 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- render_attributes() handles escaping |
| 275 |
echo $this->render_attributes( '_root' ); |
| 276 |
?> |
| 277 |
> |
| 278 |
|
| 279 |
<?php |
| 280 |
(new ProductRenderer($product))->renderGallery([ |
| 281 |
'thumb_position' => $thumbnail_position, |
| 282 |
'scrollable_thumbs' => $scrollable_thumbs, |
| 283 |
'max_thumbnails' => $max_thumbnails, |
| 284 |
]); |
| 285 |
?> |
| 286 |
|
| 287 |
</div> |
| 288 |
|
| 289 |
<?php |
| 290 |
} |
| 291 |
|
| 292 |
} |
| 293 |
|