1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SEOmatic plugin for Craft CMS 3.x |
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\debug\panels; |
13
|
|
|
|
14
|
|
|
use Craft; |
15
|
|
|
use nystudio107\seomatic\events\MetaBundleDebugDataEvent; |
16
|
|
|
use nystudio107\seomatic\helpers\MetaValue; |
17
|
|
|
use nystudio107\seomatic\models\MetaJsonLd; |
18
|
|
|
use nystudio107\seomatic\services\MetaContainers; |
19
|
|
|
use yii\base\Event; |
20
|
|
|
use yii\debug\Panel; |
21
|
|
|
|
22
|
|
|
class SeomaticPanel extends Panel |
23
|
|
|
{ |
24
|
|
|
const CONTAINER_PARSED_PROPERTIES = [ |
25
|
|
|
'metaGlobalVars', |
26
|
|
|
'metaSiteVars', |
27
|
|
|
'metaSitemapVars' |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array The accumulated MetaBundle debug data |
32
|
|
|
*/ |
33
|
|
|
protected $metaBundles = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $viewPath = '@nystudio107/seomatic/debug/views/seomatic/'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
*/ |
43
|
|
|
public function init(): void |
44
|
|
|
{ |
45
|
|
|
parent::init(); |
46
|
|
|
|
47
|
|
|
Event::on(MetaContainers::class, |
48
|
|
|
MetaContainers::EVENT_METABUNDLE_DEBUG_DATA, |
49
|
|
|
function (MetaBundleDebugDataEvent $e) { |
50
|
|
|
if ($e->metaBundle) { |
51
|
|
|
$this->metaBundles[$e->metaBundleCategory] = $e->metaBundle; |
52
|
|
|
} |
53
|
|
|
}); |
54
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritdoc |
59
|
|
|
*/ |
60
|
|
|
public function getName(): string |
61
|
|
|
{ |
62
|
|
|
return 'Seomatic'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritdoc |
67
|
|
|
*/ |
68
|
|
|
public function getSummary(): string |
69
|
|
|
{ |
70
|
|
|
return Craft::$app->getView()->render($this->viewPath . 'summary', ['panel' => $this]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritdoc |
75
|
|
|
*/ |
76
|
|
|
public function getDetail(): string |
77
|
|
|
{ |
78
|
|
|
return Craft::$app->getView()->render($this->viewPath . 'detail', ['panel' => $this]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @inheritdoc |
83
|
|
|
*/ |
84
|
|
|
public function save(): array |
85
|
|
|
{ |
86
|
|
|
$debugData = []; |
87
|
|
|
foreach ($this->metaBundles as $metaBundleCategory => $metaBundle) { |
88
|
|
|
$metaBundleArray = $metaBundle->toArray(); |
89
|
|
|
$parsedMetaBundleArray = $metaBundleArray; |
90
|
|
|
$renderedTags = []; |
91
|
|
|
// Parse the variables |
92
|
|
|
foreach (self::CONTAINER_PARSED_PROPERTIES as $property) { |
93
|
|
|
MetaValue::parseArray($parsedMetaBundleArray[$property], true, true, true); |
94
|
|
|
} |
95
|
|
|
// Render the container data as an array, and also as rendered tags |
96
|
|
|
foreach ($metaBundle->metaContainers as $metaContainerName => $metaContainer) { |
97
|
|
|
$parsedMetaBundleArray['metaContainers'][$metaContainerName]['data'] = $metaContainer->renderArray(); |
98
|
|
|
$renderedTags[$metaContainerName] = $metaContainer->render(); |
99
|
|
|
// Add in any errors from the rendering process |
100
|
|
|
foreach ($metaContainer->data as $tagName => $tag) { |
101
|
|
|
$isMetaJsonLdModel = false; |
102
|
|
|
if (is_subclass_of($tag, MetaJsonLd::class)) { |
103
|
|
|
$isMetaJsonLdModel = true; |
104
|
|
|
} |
105
|
|
|
$scenarios = [ |
106
|
|
|
'default' => 'error', |
107
|
|
|
'warning' => 'warning', |
108
|
|
|
'google' => 'warning', |
109
|
|
|
]; |
110
|
|
|
$modelScenarios = $tag->scenarios(); |
111
|
|
|
$scenarios = array_intersect_key($scenarios, $modelScenarios); |
112
|
|
|
foreach ($scenarios as $scenario => $logLevel) { |
113
|
|
|
$tag->setScenario($scenario); |
114
|
|
|
$tag->validate(); |
115
|
|
|
foreach ($tag->errors as $param => $errors) { |
116
|
|
|
/** @var array $errors */ |
117
|
|
|
foreach ($errors as $error) { |
118
|
|
|
// Change the error level depending on the error message if this is a MetaJsonLD object |
119
|
|
|
if ($isMetaJsonLdModel) { |
120
|
|
|
if (strpos($error, 'recommended') !== false) { |
121
|
|
|
$logLevel = 'warning'; |
122
|
|
|
} |
123
|
|
|
if (strpos($error, 'required') !== false |
124
|
|
|
|| strpos($error, 'Must be') !== false |
125
|
|
|
) { |
126
|
|
|
$logLevel = 'error'; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
$parsedMetaBundleArray['metaContainers'][$metaContainerName]['data'][$tagName]['__errors'][$logLevel] = $tag->getErrors(); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
$debugData[$metaBundleCategory] = [ |
136
|
|
|
'unparsed' => $metaBundleArray, |
137
|
|
|
'parsed' => $parsedMetaBundleArray, |
138
|
|
|
'renderedTags' => $renderedTags, |
139
|
|
|
]; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $debugData; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|