-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
SeoController.php
89 lines (71 loc) · 1.97 KB
/
SeoController.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
<?php
namespace ether\seo\controllers;
use craft\helpers\DateTimeHelper;
use craft\web\Controller;
use ether\seo\Seo;
class SeoController extends Controller
{
protected array|int|bool $allowAnonymous = ['robots'];
public function actionIndex ()
{
$this->renderTemplate('seo/index');
}
public function actionRobots ()
{
$settings = Seo::$i->getSettings();
\Craft::$app->response->headers->set(
'Content-Type',
'text/plain; charset=UTF-8'
);
$template = $settings->robotsTxt;
unset($settings->robotsTxt);
return $this->asRaw(\Craft::$app->view->renderString($template, [
'seo' => $settings,
]));
}
/**
* @throws \yii\web\BadRequestHttpException
* @throws \yii\base\InvalidConfigException
* @throws \Exception
*/
public function actionRenderData ()
{
$this->requirePostRequest();
$craft = \Craft::$app;
$elementType = $craft->request->getBodyParam('elementType');
$elementId = $craft->request->getBodyParam('elementId', null);
$siteId = $craft->request->getBodyParam('siteId');
$seoHandle = $craft->request->getBodyParam('seoHandle');
// Get the element
if ($elementId) {
$element = $craft->elements->getElementById(
$elementId,
$elementType,
$siteId
);
if (!$element)
return $this->asJson([]);
} else {
$element = new $elementType();
}
// Populate the data
$body = $craft->request->getBodyParams();
foreach ($body as $prop => $value)
{
if (!property_exists($element, $prop))
continue;
if (in_array($prop, ['postDate', 'expiryDate'])) {
$dateTime = DateTimeHelper::toDateTime($value);
if ($dateTime !== false) {
$element->$prop = DateTimeHelper::toDateTime($value);
}
} else {
$element->$prop = $value;
}
}
$element->setFieldValuesFromRequest(
$craft->request->getParam('fieldsLocation', 'fields')
);
return $this->asJson($element->$seoHandle->titleAsTokens);
}
}