1 | <?php |
||
2 | /** |
||
3 | * ImageOptimize plugin for Craft CMS |
||
4 | * |
||
5 | * Automatically optimize images after they've been transformed |
||
6 | * |
||
7 | * @link https://nystudio107.com |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
8 | * @copyright Copyright (c) 2021 nystudio107 |
||
0 ignored issues
–
show
|
|||
9 | */ |
||
0 ignored issues
–
show
|
|||
10 | |||
11 | namespace nystudio107\imageoptimize\helpers; |
||
12 | |||
13 | use Craft; |
||
14 | use craft\helpers\Template; |
||
15 | use craft\web\View; |
||
16 | use nystudio107\minify\Minify; |
||
17 | use yii\base\Exception; |
||
18 | |||
19 | /** |
||
0 ignored issues
–
show
|
|||
20 | * @author nystudio107 |
||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||
21 | * @package ImageOptimize |
||
0 ignored issues
–
show
|
|||
22 | * @since 1.7.0 |
||
0 ignored issues
–
show
|
|||
23 | */ |
||
0 ignored issues
–
show
|
|||
24 | class PluginTemplate |
||
25 | { |
||
26 | // Constants |
||
27 | // ========================================================================= |
||
28 | |||
29 | public const MINIFY_PLUGIN_HANDLE = 'minify'; |
||
30 | |||
31 | // Static Methods |
||
32 | // ========================================================================= |
||
33 | |||
34 | public static function renderStringTemplate(string $templateString, array $params = []): string |
||
0 ignored issues
–
show
|
|||
35 | { |
||
36 | try { |
||
37 | $html = Craft::$app->getView()->renderString($templateString, $params); |
||
38 | } catch (\Exception $e) { |
||
39 | $html = Craft::t( |
||
40 | 'image-optimize', |
||
41 | 'Error rendering template string -> {error}', |
||
42 | ['error' => $e->getMessage()] |
||
43 | ); |
||
44 | Craft::error($html, __METHOD__); |
||
45 | } |
||
46 | |||
47 | return $html; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Render a plugin template |
||
52 | * |
||
53 | * @param string $templatePath |
||
0 ignored issues
–
show
|
|||
54 | * @param array $params |
||
0 ignored issues
–
show
|
|||
55 | * @param string|null $minifier |
||
0 ignored issues
–
show
|
|||
56 | * |
||
57 | * @return string |
||
58 | */ |
||
59 | public static function renderPluginTemplate( |
||
60 | string $templatePath, |
||
61 | array $params = [], |
||
62 | string $minifier = null, |
||
63 | ): string { |
||
64 | $template = 'image-optimize/' . $templatePath; |
||
65 | $oldMode = Craft::$app->view->getTemplateMode(); |
||
66 | // Look for the template on the frontend first |
||
67 | try { |
||
68 | $templateMode = View::TEMPLATE_MODE_CP; |
||
69 | if (Craft::$app->view->doesTemplateExist($template, View::TEMPLATE_MODE_SITE)) { |
||
70 | $templateMode = View::TEMPLATE_MODE_SITE; |
||
71 | } |
||
72 | Craft::$app->view->setTemplateMode($templateMode); |
||
73 | } catch (Exception $e) { |
||
74 | Craft::error($e->getMessage(), __METHOD__); |
||
75 | } |
||
76 | |||
77 | // Render the template with our vars passed in |
||
78 | try { |
||
79 | $htmlText = Craft::$app->view->renderTemplate($template, $params); |
||
80 | if ($minifier) { |
||
81 | // If Minify is installed, use it to minify the template |
||
82 | $minify = Craft::$app->getPlugins()->getPlugin(self::MINIFY_PLUGIN_HANDLE); |
||
83 | if ($minify) { |
||
84 | $htmlText = Minify::$plugin->minify->$minifier($htmlText); |
||
85 | } |
||
86 | } |
||
87 | } catch (\Exception $e) { |
||
88 | $htmlText = Craft::t( |
||
89 | 'image-optimize', |
||
90 | 'Error rendering `{template}` -> {error}', |
||
91 | ['template' => $templatePath, 'error' => $e->getMessage()] |
||
92 | ); |
||
93 | Craft::error($htmlText, __METHOD__); |
||
94 | } |
||
95 | |||
96 | // Restore the old template mode |
||
97 | try { |
||
98 | Craft::$app->view->setTemplateMode($oldMode); |
||
99 | } catch (Exception $e) { |
||
100 | Craft::error($e->getMessage(), __METHOD__); |
||
101 | } |
||
102 | |||
103 | return Template::raw($htmlText); |
||
104 | } |
||
105 | } |
||
106 |