|
1 | 1 | <?php |
2 | 2 |
|
3 | | -/** |
4 | | - * PHP MJML Renderer library |
5 | | - * |
6 | | - * @package MadeByDenis\PhpMjmlRenderer |
7 | | - * @link https://github.com/dingo-d/php-mjml-renderer |
8 | | - * @license https://opensource.org/licenses/MIT MIT |
9 | | - */ |
10 | | - |
11 | 3 | declare(strict_types=1); |
12 | 4 |
|
13 | 5 | namespace MadeByDenis\PhpMjmlRenderer\Elements\HeadComponents; |
14 | 6 |
|
15 | 7 | use MadeByDenis\PhpMjmlRenderer\Elements\AbstractElement; |
16 | | -use MadeByDenis\PhpMjmlRenderer\Elements\ElementFactory; |
17 | 8 |
|
18 | | -/** |
19 | | - * Mjml Head Element |
20 | | - * |
21 | | - * @link https://documentation.mjml.io/#mj-head |
22 | | - * |
23 | | - * @since 1.0.0 |
24 | | - */ |
25 | 9 | class MjHead extends AbstractElement |
26 | 10 | { |
27 | 11 | public const string TAG_NAME = 'mj-head'; |
28 | | - |
29 | 12 | public const bool ENDING_TAG = false; |
30 | 13 |
|
31 | | - /** |
32 | | - * List of allowed attributes on the element |
33 | | - * |
34 | | - * @var array<string, array<string, string>> |
35 | | - */ |
36 | 14 | protected array $allowedAttributes = []; |
37 | | - |
38 | 15 | protected array $defaultAttributes = []; |
39 | 16 |
|
40 | 17 | public function render(): string |
41 | 18 | { |
42 | 19 | $children = $this->getChildren() ?? []; |
43 | | - |
44 | | - $title = ''; |
45 | | - $preview = ''; |
46 | | - $styles = ''; |
47 | | - $fonts = []; |
48 | | - $breakpoint = '480px'; |
49 | | - |
50 | | - // Process head components |
51 | | - foreach ($children as $child) { |
52 | | - $tag = $child->getTag(); |
53 | | - $element = ElementFactory::create($child); |
54 | | - |
55 | | - match ($tag) { |
56 | | - 'mj-title' => $title = $element->render(), |
57 | | - 'mj-preview' => $preview = $element->render(), |
58 | | - 'mj-style' => $styles .= $element->render(), |
59 | | - 'mj-font' => $fonts[] = $element->render(), |
60 | | - 'mj-breakpoint' => $breakpoint = $child->getAttributeValue('width') ?: '480px', |
61 | | - default => null, |
62 | | - }; |
63 | | - } |
64 | | - |
65 | | - // Build head content |
66 | | - $head = '<head>'; |
67 | | - $head .= $title ?: '<title></title>'; |
68 | | - $head .= '<!--[if !mso]><!--><meta http-equiv="X-UA-Compatible" content="IE=edge"><!--<![endif]-->'; |
69 | | - $head .= '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'; |
70 | | - $head .= '<meta name="viewport" content="width=device-width,initial-scale=1">'; |
71 | | - $head .= $this->renderBaseStyles(); |
72 | | - $head .= $this->renderMsoStyles(); |
73 | | - $head .= $this->renderOutlookStyles(); |
74 | | - |
75 | | - // Add fonts |
76 | | - foreach ($fonts as $font) { |
77 | | - $head .= $font; |
78 | | - } |
79 | | - |
80 | | - // Add responsive styles |
81 | | - $head .= $this->renderResponsiveStyles($breakpoint); |
82 | | - |
83 | | - // Add custom styles |
84 | | - $head .= $styles; |
85 | | - |
86 | | - // Add preview |
87 | | - $head .= $preview; |
88 | | - |
89 | | - $head .= '</head>'; |
90 | | - |
91 | | - return $head; |
92 | | - } |
93 | | - |
94 | | - private function renderBaseStyles(): string |
95 | | - { |
96 | | - $styles = '<style type="text/css">#outlook a { padding:0; }' . "\n"; |
97 | | - $styles .= ' body { margin:0;padding:0;'; |
98 | | - $styles .= '-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%; }' . "\n"; |
99 | | - $styles .= ' table, td { border-collapse:collapse;'; |
100 | | - $styles .= 'mso-table-lspace:0pt;mso-table-rspace:0pt; }' . "\n"; |
101 | | - $styles .= ' img { border:0;height:auto;line-height:100%; outline:none;'; |
102 | | - $styles .= 'text-decoration:none;-ms-interpolation-mode:bicubic; }' . "\n"; |
103 | | - $styles .= ' p { display:block;margin:13px 0; }</style>'; |
104 | | - |
105 | | - return $styles; |
106 | | - } |
107 | | - |
108 | | - private function renderMsoStyles(): string |
109 | | - { |
110 | | - $msoStyles = '<!--[if mso]>'; |
111 | | - $msoStyles .= '<noscript><xml><o:OfficeDocumentSettings>'; |
112 | | - $msoStyles .= '<o:AllowPNG/><o:PixelsPerInch>96</o:PixelsPerInch>'; |
113 | | - $msoStyles .= '</o:OfficeDocumentSettings></xml></noscript>'; |
114 | | - $msoStyles .= '<![endif]-->'; |
115 | | - |
116 | | - return $msoStyles; |
117 | | - } |
118 | | - |
119 | | - private function renderOutlookStyles(): string |
120 | | - { |
121 | | - return '<!--[if lte mso 11]> |
122 | | - <style type="text/css"> |
123 | | - .mj-outlook-group-fix { width:100% !important; } |
124 | | - </style> |
125 | | - <![endif]-->'; |
126 | | - } |
127 | | - |
128 | | - private function renderResponsiveStyles(string $breakpoint): string |
129 | | - { |
130 | | - $minWidth = $breakpoint; |
131 | | - |
132 | | - $styles = '<!--[if !mso]><!-->'; |
133 | | - $styles .= '<link href="https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700" '; |
134 | | - $styles .= 'rel="stylesheet" type="text/css">'; |
135 | | - $styles .= '<style type="text/css">'; |
136 | | - $styles .= '@import url(https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700);'; |
137 | | - $styles .= '</style><!--<![endif]-->'; |
138 | | - $styles .= "<style type=\"text/css\">@media only screen and (min-width:$minWidth) {"; |
139 | | - $styles .= ' .mj-column-per-100 { width:100% !important; max-width: 100%; }'; |
140 | | - $styles .= ' }</style>'; |
141 | | - $styles .= "<style media=\"screen and (min-width:$minWidth)\">"; |
142 | | - $styles .= '.moz-text-html .mj-column-per-100 { width:100% !important; max-width: 100%; }'; |
143 | | - $styles .= '</style><style type="text/css"></style>'; |
144 | | - |
145 | | - return $styles; |
| 20 | + return $this->renderChildren($children, []); |
146 | 21 | } |
147 | 22 |
|
148 | | - /** |
149 | | - * @return array<string, array<string, string>> |
150 | | - */ |
151 | 23 | public function getStyles(): array |
152 | 24 | { |
153 | 25 | return []; |
|
0 commit comments