| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\Renderer; |
| 4 |
|
| 5 |
use FluentCart\Framework\Support\Arr; |
| 6 |
|
| 7 |
class ProductListRenderer |
| 8 |
{ |
| 9 |
protected $products = []; |
| 10 |
|
| 11 |
protected $listTitle = null; |
| 12 |
|
| 13 |
protected $wrapperClass = null; |
| 14 |
|
| 15 |
protected $cursor = null; |
| 16 |
|
| 17 |
protected $columns = 4; |
| 18 |
|
| 19 |
protected $hideExcerpt = false; |
| 20 |
|
| 21 |
public function __construct($products, $listTitle = null, $wrapperClass = null, $config = []) |
| 22 |
{ |
| 23 |
$this->products = $products; |
| 24 |
$this->listTitle = $listTitle; |
| 25 |
$this->wrapperClass = $wrapperClass; |
| 26 |
$columns = Arr::get($config, 'columns', 4); |
| 27 |
$this->columns = max(1, min(6, intval($columns))); |
| 28 |
$this->hideExcerpt = Arr::get($config, 'hide_excerpt', false); |
| 29 |
|
| 30 |
if($products instanceof \FluentCart\Framework\Pagination\CursorPaginator){ |
| 31 |
$this->cursor = wp_parse_args(wp_parse_url($products->nextPageUrl(), PHP_URL_QUERY)); |
| 32 |
$this->cursor = Arr::get($this->cursor, 'cursor', ''); |
| 33 |
} |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
public function render() |
| 38 |
{ |
| 39 |
if ( |
| 40 |
(is_array($this->products) && empty($this->products)) || |
| 41 |
($this->products instanceof \FluentCart\Framework\Pagination\CursorPaginator && $this->products->count() === 0) |
| 42 |
) { |
| 43 |
return ''; |
| 44 |
} |
| 45 |
|
| 46 |
$columns = $this->columns |
| 47 |
? 'style="--fct-product-list-columns: ' . $this->columns . ';"' |
| 48 |
: ''; |
| 49 |
|
| 50 |
?> |
| 51 |
<section class="fct-product-list-container <?php echo esc_attr($this->wrapperClass); ?>" aria-label="<?php echo esc_attr($this->listTitle ?: __('Product List', 'fluent-cart')); ?>"> |
| 52 |
<?php $this->renderTitle(); ?> |
| 53 |
<div |
| 54 |
class="fct-product-list" |
| 55 |
role="list" |
| 56 |
aria-live="polite" |
| 57 |
aria-busy="false" |
| 58 |
<?php echo $columns; ?> |
| 59 |
> |
| 60 |
<?php $this->renderProductList(); ?> |
| 61 |
</div> |
| 62 |
</section> |
| 63 |
<?php |
| 64 |
} |
| 65 |
|
| 66 |
public function renderProductList() |
| 67 |
{ |
| 68 |
|
| 69 |
foreach ($this->products as $index => $product) { |
| 70 |
$config = ['hide_excerpt' => $this->hideExcerpt]; |
| 71 |
if($index == 0 && $this->cursor){ |
| 72 |
$config['cursor'] = $this->cursor; |
| 73 |
} |
| 74 |
?> |
| 75 |
<div |
| 76 |
class="fct-product-list-item" |
| 77 |
role="listitem" |
| 78 |
> |
| 79 |
<?php (new ProductCardRender($product, $config))->render(); ?> |
| 80 |
</div> |
| 81 |
|
| 82 |
<?php |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
public function renderTitle() { |
| 87 |
|
| 88 |
if(!empty($this->listTitle)) : ?> |
| 89 |
<h4 class="fct-product-list-heading"> |
| 90 |
<?php echo esc_html($this->listTitle); ?> |
| 91 |
</h4> |
| 92 |
<?php endif; |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
} |
| 97 |
|