| 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 |
public function __construct($products, $listTitle = null, $wrapperClass = null) |
| 18 |
{ |
| 19 |
$this->products = $products; |
| 20 |
$this->listTitle = $listTitle; |
| 21 |
$this->wrapperClass = $wrapperClass; |
| 22 |
|
| 23 |
if($products instanceof \FluentCart\Framework\Pagination\CursorPaginator){ |
| 24 |
$this->cursor = wp_parse_args(wp_parse_url($products->nextPageUrl(), PHP_URL_QUERY)); |
| 25 |
$this->cursor = Arr::get($this->cursor, 'cursor', ''); |
| 26 |
} |
| 27 |
|
| 28 |
} |
| 29 |
|
| 30 |
public function render() |
| 31 |
{ |
| 32 |
if ( |
| 33 |
(is_array($this->products) && empty($this->products)) || |
| 34 |
($this->products instanceof \FluentCart\Framework\Pagination\CursorPaginator && $this->products->count() === 0) |
| 35 |
) { |
| 36 |
return ''; |
| 37 |
} |
| 38 |
?> |
| 39 |
<section class="fct-product-list-container <?php echo esc_attr($this->wrapperClass); ?>" aria-label="<?php echo esc_attr($this->listTitle ?: __('Product List', 'fluent-cart')); ?>"> |
| 40 |
<?php $this->renderTitle(); ?> |
| 41 |
<div |
| 42 |
class="fct-product-list" |
| 43 |
role="list" |
| 44 |
aria-live="polite" |
| 45 |
aria-busy="false" |
| 46 |
> |
| 47 |
<?php $this->renderProductList(); ?> |
| 48 |
</div> |
| 49 |
</section> |
| 50 |
<?php |
| 51 |
} |
| 52 |
|
| 53 |
public function renderProductList() |
| 54 |
{ |
| 55 |
|
| 56 |
foreach ($this->products as $index => $product) { |
| 57 |
$config = []; |
| 58 |
if($index == 0 && $this->cursor){ |
| 59 |
$config['cursor'] = $this->cursor; |
| 60 |
} |
| 61 |
?> |
| 62 |
<div |
| 63 |
class="fct-product-list-item" |
| 64 |
role="listitem" |
| 65 |
> |
| 66 |
<?php (new ProductCardRender($product, $config))->render(); ?> |
| 67 |
</div> |
| 68 |
|
| 69 |
<?php |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
public function renderTitle() { |
| 74 |
|
| 75 |
if(!empty($this->listTitle)) : ?> |
| 76 |
<h4 class="fct-product-list-heading"> |
| 77 |
<?php echo esc_html($this->listTitle); ?> |
| 78 |
</h4> |
| 79 |
<?php endif; |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
} |
| 84 |
|