-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstyla.php
271 lines (211 loc) · 7.84 KB
/
styla.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
/*
* Copyright (C) 2019 Styla GmbH. All rights reserved.
*/
class Styla {
public static $seoPrefix = "https://seo.styla.com/clients/";
public static $cdnPrefix = "https://client-scripts.styla.com/";
public static $versionPrefix = "https://live.styla.com/api/version/";
public static $prophetUrl = "https://engine.styla.com/init.js";
public $clientName = null;
public $contentKey = null;
public $rootPath = null;
private $seo = null;
private $contentId = null;
private $slotId = null;
private $useCurl = false;
private $forceRootPath = false;
public function __construct(
$clientName,
$params = []
) {
$this->clientName = $clientName;
$this->contentId = $params["content"] ? $params["content"] : null;
$this->slotId = $params["slot"] ? $params[""] : null;
$this->rootPath = $params["rootPath"] ? $params["rootPath"] : null;
$this->useCurl = in_array('curl', get_loaded_extensions());
$this->forceRootPath = $params["forceRootPath"] ? $params["forceRootPath"] : false;
$this->init();
}
public function getMagazineMetaTags() {
$scriptSource = join([
self::$cdnPrefix,
"scripts/clients/",
$this->clientName,
".js"
]);
$styleSource = join([
self::$cdnPrefix,
"styles/clients/",
$this->clientName,
".css"
]);
return join([
"<link rel=\"preconnect\" href=\"https://engine-nle.styla.com\">",
"<link rel=\"preconnect\" href=\"https://config.styla.com\">",
"<link rel=\"preconnect\" href=\"https://static-cdn.styla.com\">",
"<link rel=\"preconnect\" href=\"https://styla-prod-us.imgix.net\">",
"<script src=\"",
$scriptSource,
"\"></script>",
"<link rel=\"stylesheet\" type=\"text/css\" href=\"",
$styleSource,
"\">"
]);
}
public function getLPMetaTags() {
return join([
"<link rel=\"preconnect\" href=\"https://engine.styla.com\">",
"<script src=\"", self::$prophetUrl, "\" async></script>"
]);
}
public function getMagazineTag() {
return join([
"<div id=\"stylaMagazine\">",
$this->getSEOBody(),
"</div>"
]);
}
public function getLPTag($params = []) {
$client = $this->clientName;
$content = $this->contentId;
$slot = $this->slotId;
if ( $params["client"] ) {
$client = $params["client"];
}
if ( $params["content"] ) {
$content = $params["content"];
}
if ( $params["slot"] ) {
$slot = $params["slot"];
}
return join(' ', [
"<div",
$client ? "data-styla-client=\"" . $client . "\"" : "",
$content ? "data-styla-content=\"" . $content . "\"" : "",
$slot ? "data-styla-slot=\"" . $slot . "\"" : "",
$params["noClosedTag"] ? ">" : "></div>"
]);
}
public function getLPTagWithSeo($params = []) {
$params["noClosedTag"] = true;
return join([
$this->getLPTag($params),
$this->getSEOBody(),
"</div>"
]);
}
public function getSEOHead() {
$this->getSEOContent();
if ( $this->seo === null ) {
return '';
}
if ( !isset( $this->seo->html->head ) ) {
return '';
}
return $this->seo->html->head;
}
public function getSEOBody() {
$this->getSEOContent();
if ( $this->seo === null ) {
return '';
}
if ( !isset( $this->seo->html->body ) ) {
return '';
}
return $this->seo->html->body;
}
public function getSEOContent() {
if ( $this->seo !== null ) {
return $this->seo;
}
$resolvedContentKey = $this->contentKey;
$isLP = $this->contentId !== null;
if ( $isLP ) {
$resolvedContentKey = $this->contentId;
}
if ( $this->forceRootPath && isset($this->rootPath) ) {
$resolvedContentKey = join([
$this->contentKey,
$resolvedContentKey
]);
}
// build URL to fetch data from
$url = join([
self::$seoPrefix,
$this->clientName,
"?",
$isLP ? "type=area&" : "",
"url=",
$resolvedContentKey
]);
$this->seo = $this->fetchSEOContent($url);
return $this->seo;
}
private function fetchSEOContent($url) {
// If not in cache yet -> fetch SEO information for current content
$rawSeoResponse = $this->fetchResource($url);
// Check the SEO API response
if(!$rawSeoResponse) {
return;
}
// Transform response to JSON
$seoResponse = json_decode($rawSeoResponse);
// Check if response is really an oci_fetch_object
if(!is_object($seoResponse)) {
return;
}
// Check if json has status code
if(!isset($seoResponse->status)) {
return;
}
// check if response code is 2XX
if($seoResponse->status !== 200) {
return;
}
return $seoResponse;
}
private function fetchResource($url) {
if (!$this->useCurl) {
return file_get_contents($url);
}
$req = curl_init();
curl_setopt($req, CURLOPT_HEADER, false);
curl_setopt($req, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($req, CURLOPT_URL, $url);
curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($req);
curl_close($req);
return $res;
}
// Extract 'offset' param from given URL object
private function getOffsetParam() {
$url = parse_url($_SERVER['REQUEST_URI']);
$query = isset($url['query']) ? $url['query'] : "";
if(strpos($query, 'offset=') === false) {
return '';
}
return $query;
}
private function buildContentKey() {
if ( $this->forceRootPath && isset($this->rootPath) ) {
$this->contentKey = $this->rootPath;
return;
}
$url = parse_url($_SERVER['REQUEST_URI']);
$currPath = $url['path'];
$contentKey = isset($_REQUEST["origUrl"]) ? $_REQUEST["origUrl"] : "/";
if($this->rootPath !== "/"){
$path = $this->rootPath[0] !== "/" ? "/".$this->rootPath : $this->rootPath;
// search for rootPath in $path and remove it
$contentKey = str_replace($path, "", $currPath);
}
// get offset of current URL
$offset = $this->getOffsetParam();
$offsetKeySuffix = !empty($offset) ? "&" . $offset : "";
$this->contentKey = join([ $contentKey, $offsetKeySuffix ]);
}
private function init() {
$this->buildContentKey();
}
}