PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.25
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.25
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
fluent-cart / app / Hooks / Handlers / ShortCodes / ShortCode.php

ShortCode.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.25, at app/Hooks/Handlers/ShortCodes/ShortCode.php

73 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Hooks\Handlers\ShortCodes;
4
5 use FluentCart\Api\Contracts\CanEnqueue;
6 use FluentCart\App\App;
7 use FluentCart\Framework\Support\Arr;
8 use FluentCart\Framework\Support\Str;
9
10 abstract class ShortCode
11 {
12 use CanEnqueue;
13
14 protected static string $shortCodeName;
15 protected ?array $shortCodeAttributes = null;
16 protected ?string $slugPrefix = null;
17
18 public function __construct(array $shortcodeAttributes = [])
19 {
20 $this->shortCodeAttributes = $this->parseAttribute($shortcodeAttributes);
21 $this->slugPrefix = App::config()->get('app.slug');
22 }
23
24 abstract public function render(?array $viewData = null);
25
26
27 public function renderShortcode($block = null)
28 {
29 $this->enqueueAssets();
30 ob_start(null);
31 $this->render(
32 $this->viewData()
33 );
34 return ob_get_clean();
35 }
36
37 public function parseAttribute(array $shortcodeAttributes): array
38 {
39 return $shortcodeAttributes;
40 }
41
42 public function viewData(): ?array
43 {
44 return $this->shortCodeAttributes;
45 }
46
47 public static function getShortCodeName(): string
48 {
49 return static::$shortCodeName;
50 }
51
52 public static function register()
53 {
54 add_shortcode(static::getShortCodeName(), function ($shortcodeAttributes, $content, $block) {
55 return static::make($shortcodeAttributes)->renderShortcode($block);
56 });
57 }
58
59 protected function generateEnqueueSlug(): string
60 {
61 return Str::of(
62 $this->slugPrefix . '_' . static::getShortCodeName()
63 )->snake('')->replace('-', '_')->toString();
64 }
65
66 public static function make($shortcodeAttributes = null): ShortCode
67 {
68 return new static(
69 Arr::wrap($shortcodeAttributes)
70 );
71 }
72 }
73