|
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\helpers; |
|
13
|
|
|
|
|
14
|
|
|
use Craft; |
|
15
|
|
|
|
|
16
|
|
|
use craft\helpers\Template; |
|
17
|
|
|
|
|
18
|
|
|
use craft\web\View; |
|
19
|
|
|
use nystudio107\minify\Minify; |
|
20
|
|
|
use nystudio107\seomatic\Seomatic; |
|
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
|
|
|
public 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
|
|
|
* Render a plugin template |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $templatePath |
|
66
|
|
|
* @param array $params |
|
67
|
|
|
* @param string|null $minifier |
|
68
|
|
|
* |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
|
|
public static function renderPluginTemplate( |
|
72
|
|
|
string $templatePath, |
|
73
|
|
|
array $params = [], |
|
74
|
|
|
string $minifier = null, |
|
75
|
|
|
): string { |
|
76
|
|
|
$template = 'seomatic/' . $templatePath; |
|
77
|
|
|
$oldMode = Craft::$app->view->getTemplateMode(); |
|
78
|
|
|
// Look for the template on the frontend first |
|
79
|
|
|
try { |
|
80
|
|
|
$templateMode = View::TEMPLATE_MODE_CP; |
|
81
|
|
|
if (Craft::$app->view->doesTemplateExist($template, View::TEMPLATE_MODE_SITE)) { |
|
82
|
|
|
$templateMode = View::TEMPLATE_MODE_SITE; |
|
83
|
|
|
} |
|
84
|
|
|
Craft::$app->view->setTemplateMode($templateMode); |
|
85
|
|
|
} catch (Exception $e) { |
|
86
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// Render the template with our vars passed in |
|
90
|
|
|
try { |
|
91
|
|
|
$htmlText = Craft::$app->view->renderTemplate($template, $params); |
|
92
|
|
|
if ($minifier) { |
|
93
|
|
|
// If Minify is installed, use it to minify the template |
|
94
|
|
|
$minify = Craft::$app->getPlugins()->getPlugin(self::MINIFY_PLUGIN_HANDLE); |
|
95
|
|
|
if ($minify) { |
|
96
|
|
|
$htmlText = Minify::$plugin->minify->$minifier($htmlText); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} catch (\Exception $e) { |
|
100
|
|
|
$htmlText = Craft::t( |
|
101
|
|
|
'seomatic', |
|
102
|
|
|
'Error rendering `{template}` -> {error}', |
|
103
|
|
|
['template' => $templatePath, 'error' => $e->getMessage()] |
|
104
|
|
|
); |
|
105
|
|
|
Craft::error($htmlText, __METHOD__); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
// Restore the old template mode |
|
109
|
|
|
try { |
|
110
|
|
|
Craft::$app->view->setTemplateMode($oldMode); |
|
111
|
|
|
} catch (Exception $e) { |
|
112
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return Template::raw($htmlText); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.