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\helpers; |
13
|
|
|
|
14
|
|
|
use nystudio107\seomatic\Seomatic; |
15
|
|
|
|
16
|
|
|
use nystudio107\minify\Minify; |
|
|
|
|
17
|
|
|
|
18
|
|
|
use Craft; |
19
|
|
|
use craft\helpers\Template; |
20
|
|
|
use craft\web\View; |
21
|
|
|
|
22
|
|
|
use yii\base\Exception; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author nystudio107 |
|
|
|
|
26
|
|
|
* @package Seomatic |
27
|
|
|
* @since 3.0.0 |
28
|
|
|
*/ |
29
|
|
|
class PluginTemplate |
30
|
|
|
{ |
31
|
|
|
// Constants |
32
|
|
|
// ========================================================================= |
33
|
|
|
|
34
|
|
|
const MINIFY_PLUGIN_HANDLE = 'minify'; |
35
|
|
|
|
36
|
|
|
// Static Methods |
37
|
|
|
// ========================================================================= |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Render the passed in Twig string template, catching any errors |
41
|
|
|
* |
42
|
|
|
* @param string $templateString |
|
|
|
|
43
|
|
|
* @param array $params |
|
|
|
|
44
|
|
|
* @return string |
|
|
|
|
45
|
|
|
*/ |
46
|
|
|
public static function renderStringTemplate(string $templateString, array $params = []): string |
47
|
|
|
{ |
48
|
|
|
try { |
49
|
|
|
$html = Seomatic::$view->renderString($templateString, $params); |
50
|
|
|
} catch (\Exception $e) { |
51
|
|
|
$html = Craft::t( |
52
|
|
|
'seomatic', |
53
|
|
|
'Error rendering template string -> {error}', |
54
|
|
|
['error' => $e->getMessage()] |
55
|
|
|
); |
56
|
|
|
Craft::error($html, __METHOD__); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $html; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Return an error if the Twig template is invalid, or an empty string if no errors |
64
|
|
|
* |
65
|
|
|
* @param string $templateString |
|
|
|
|
66
|
|
|
* @param array $params |
|
|
|
|
67
|
|
|
* @return string |
|
|
|
|
68
|
|
|
*/ |
69
|
|
|
public static function isStringTemplateValid(string $templateString, array $params = []): string |
70
|
|
|
{ |
71
|
|
|
$error = ''; |
72
|
|
|
try { |
73
|
|
|
$html = Seomatic::$view->renderString($templateString, $params); |
|
|
|
|
74
|
|
|
} catch (\Exception $e) { |
75
|
|
|
$error = Craft::t( |
76
|
|
|
'seomatic', |
77
|
|
|
'Error rendering template string -> {error}', |
78
|
|
|
['error' => $e->getMessage()] |
79
|
|
|
); |
80
|
|
|
Craft::error($error, __METHOD__); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $error; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Render a plugin template |
88
|
|
|
* |
89
|
|
|
* @param string $templatePath |
|
|
|
|
90
|
|
|
* @param array $params |
|
|
|
|
91
|
|
|
* @param string|null $minifier |
|
|
|
|
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public static function renderPluginTemplate( |
96
|
|
|
string $templatePath, |
97
|
|
|
array $params = [], |
98
|
|
|
string $minifier = null |
99
|
|
|
): string { |
100
|
|
|
$template = 'seomatic/' . $templatePath; |
101
|
|
|
$oldMode = Craft::$app->view->getTemplateMode(); |
102
|
|
|
// Look for the template on the frontend first |
103
|
|
|
try { |
104
|
|
|
$templateMode = View::TEMPLATE_MODE_CP; |
105
|
|
|
if (Craft::$app->view->doesTemplateExist($template, View::TEMPLATE_MODE_SITE)) { |
106
|
|
|
$templateMode = View::TEMPLATE_MODE_SITE; |
107
|
|
|
} |
108
|
|
|
Craft::$app->view->setTemplateMode($templateMode); |
109
|
|
|
} catch (Exception $e) { |
110
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// Render the template with our vars passed in |
114
|
|
|
try { |
115
|
|
|
$htmlText = Craft::$app->view->renderTemplate($template, $params); |
116
|
|
|
if ($minifier) { |
117
|
|
|
// If Minify is installed, use it to minify the template |
118
|
|
|
$minify = Craft::$app->getPlugins()->getPlugin(self::MINIFY_PLUGIN_HANDLE); |
119
|
|
|
if ($minify) { |
120
|
|
|
$htmlText = Minify::$plugin->minify->$minifier($htmlText); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
} catch (\Exception $e) { |
125
|
|
|
$htmlText = Craft::t( |
126
|
|
|
'seomatic', |
127
|
|
|
'Error rendering `{template}` -> {error}', |
128
|
|
|
['template' => $templatePath, 'error' => $e->getMessage()] |
129
|
|
|
); |
130
|
|
|
Craft::error($htmlText, __METHOD__); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// Restore the old template mode |
134
|
|
|
try { |
135
|
|
|
Craft::$app->view->setTemplateMode($oldMode); |
136
|
|
|
} catch (Exception $e) { |
137
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return Template::raw($htmlText); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths