1 | <?php |
||
2 | /** |
||
3 | * SEOmatic plugin for Craft CMS |
||
4 | * |
||
5 | * A turnkey SEO implementation for Craft CMS that is comprehensive, powerful, |
||
6 | * and flexible |
||
7 | * |
||
8 | * @link https://nystudio107.com |
||
9 | * @copyright Copyright (c) 2017 nystudio107 |
||
10 | */ |
||
11 | |||
12 | namespace nystudio107\seomatic\models\metatag; |
||
13 | |||
14 | use Craft; |
||
15 | use nystudio107\seomatic\models\MetaTag; |
||
16 | use nystudio107\seomatic\services\Helper as SeomaticHelper; |
||
17 | use yii\web\BadRequestHttpException; |
||
18 | |||
19 | /** |
||
20 | * @author nystudio107 |
||
21 | * @package Seomatic |
||
22 | * @since 3.0.0 |
||
23 | */ |
||
24 | class RobotsTag extends MetaTag |
||
25 | { |
||
26 | // Constants |
||
27 | // ========================================================================= |
||
28 | |||
29 | public const ITEM_TYPE = 'RobotsTag'; |
||
30 | |||
31 | // Static Methods |
||
32 | // ========================================================================= |
||
33 | |||
34 | // Public Properties |
||
35 | // ========================================================================= |
||
36 | |||
37 | // Public Methods |
||
38 | // ========================================================================= |
||
39 | |||
40 | /** |
||
41 | * @inheritdoc |
||
42 | */ |
||
43 | public function init(): void |
||
44 | { |
||
45 | // Handle a bug where 'content' could be permanently set to 'none' |
||
46 | if ($this->content === 'none') { |
||
47 | $this->content = $this->environment['live']['content'] ?? '{{ seomatic.meta.robots }}'; |
||
48 | } |
||
49 | parent::init(); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @inheritdoc |
||
54 | */ |
||
55 | public function rules(): array |
||
56 | { |
||
57 | $rules = parent::rules(); |
||
58 | $rules = array_merge($rules, [ |
||
59 | // Robots tags have specific content attributes |
||
60 | [ |
||
61 | 'content', 'in', 'range' => [ |
||
62 | 'all', |
||
63 | 'index', |
||
64 | 'noindex', |
||
65 | 'follow', |
||
66 | 'nofollow', |
||
67 | 'none', |
||
68 | 'noodp', |
||
69 | 'noarchive', |
||
70 | 'nosnippet', |
||
71 | 'noimageindex', |
||
72 | 'nocache', |
||
73 | ], 'on' => ['warning'], |
||
74 | ], |
||
75 | ]); |
||
76 | |||
77 | return $rules; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @inheritdoc |
||
82 | */ |
||
83 | public function prepForRender(&$data): bool |
||
84 | { |
||
85 | $shouldRender = parent::prepForRender($data); |
||
86 | if ($shouldRender) { |
||
87 | // Set meta robots tag to `none` for http status codes >= 400 |
||
88 | $request = Craft::$app->getRequest(); |
||
89 | $response = Craft::$app->getResponse(); |
||
90 | if (!$request->isConsoleRequest) { |
||
91 | if ($response->statusCode >= 400) { |
||
92 | $data['content'] = 'none'; |
||
93 | } |
||
94 | try { |
||
95 | if ($request->getToken() !== null) { |
||
96 | $data['content'] = 'none'; |
||
97 | } |
||
98 | } catch (BadRequestHttpException $e) { |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
Loading history...
|
|||
99 | } |
||
100 | } |
||
101 | // Set meta robots tag to `none` if this is any kind of Craft preview |
||
102 | if (SeomaticHelper::isPreview()) { |
||
103 | $data['content'] = 'none'; |
||
104 | } |
||
105 | } |
||
106 | |||
107 | return $shouldRender; |
||
108 | } |
||
109 | } |
||
110 |