nystudio107 /
craft-youtubeliveembed
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * YouTube Live Embed plugin for Craft CMS |
||
| 4 | * |
||
| 5 | * This plugin allows you to embed a YouTube live stream and/or live chat on your webpage |
||
| 6 | * |
||
| 7 | * @link https://nystudio107.com |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 8 | * @copyright Copyright (c) 2019 nystudio107 |
||
|
0 ignored issues
–
show
|
|||
| 9 | */ |
||
|
0 ignored issues
–
show
|
|||
| 10 | |||
| 11 | namespace nystudio107\youtubeliveembed\helpers; |
||
| 12 | |||
| 13 | use Craft; |
||
| 14 | use craft\helpers\Template; |
||
| 15 | use craft\web\View; |
||
| 16 | use Twig\Markup; |
||
| 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]>"
Loading history...
|
|||
| 21 | * @package YoutubeLiveEmbed |
||
|
0 ignored issues
–
show
|
|||
| 22 | * @since 1.0.0 |
||
|
0 ignored issues
–
show
|
|||
| 23 | */ |
||
|
0 ignored issues
–
show
|
|||
| 24 | class PluginTemplate |
||
| 25 | { |
||
| 26 | // Static Methods |
||
| 27 | // ========================================================================= |
||
| 28 | |||
| 29 | public static function renderStringTemplate(string $templateString, array $params = []): string |
||
|
0 ignored issues
–
show
|
|||
| 30 | { |
||
| 31 | try { |
||
| 32 | $html = Craft::$app->getView()->renderString($templateString, $params); |
||
| 33 | } catch (\Exception $e) { |
||
| 34 | $html = Craft::t( |
||
| 35 | 'youtubeliveembed', |
||
| 36 | 'Error rendering template string -> {error}', |
||
| 37 | ['error' => $e->getMessage()] |
||
| 38 | ); |
||
| 39 | Craft::error($html, __METHOD__); |
||
| 40 | } |
||
| 41 | |||
| 42 | return $html; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Render a plugin template |
||
| 47 | * |
||
| 48 | * @param $templatePath |
||
|
0 ignored issues
–
show
|
|||
| 49 | * @param $params |
||
|
0 ignored issues
–
show
|
|||
| 50 | * |
||
| 51 | * @return Markup |
||
| 52 | */ |
||
| 53 | public static function renderPluginTemplate(string $templatePath, array $params = []): Markup |
||
| 54 | { |
||
| 55 | // Stash the old template mode, and set it Control Panel template mode |
||
| 56 | $oldMode = Craft::$app->view->getTemplateMode(); |
||
| 57 | try { |
||
| 58 | Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_CP); |
||
| 59 | } catch (Exception $e) { |
||
| 60 | Craft::error($e->getMessage(), __METHOD__); |
||
| 61 | } |
||
| 62 | |||
| 63 | // Render the template with our vars passed in |
||
| 64 | try { |
||
| 65 | $htmlText = Craft::$app->view->renderTemplate('youtubeliveembed/' . $templatePath, $params); |
||
| 66 | $templateRendered = true; |
||
| 67 | } catch (\Exception $e) { |
||
| 68 | $htmlText = Craft::t( |
||
| 69 | 'youtubeliveembed', |
||
| 70 | 'Error rendering `{template}` -> {error}', |
||
| 71 | ['template' => $templatePath, 'error' => $e->getMessage()] |
||
| 72 | ); |
||
| 73 | Craft::error($htmlText, __METHOD__); |
||
| 74 | $templateRendered = false; |
||
| 75 | } |
||
| 76 | |||
| 77 | // If we couldn't find a plugin template, look for a frontend template |
||
| 78 | if (!$templateRendered) { |
||
| 79 | try { |
||
| 80 | Craft::$app->view->setTemplateMode(View::TEMPLATE_MODE_SITE); |
||
| 81 | } catch (Exception $e) { |
||
| 82 | Craft::error($e->getMessage(), __METHOD__); |
||
| 83 | } |
||
| 84 | // Render the template with our vars passed in |
||
| 85 | try { |
||
| 86 | $htmlText = Craft::$app->view->renderTemplate($templatePath, $params); |
||
| 87 | $templateRendered = true; |
||
|
0 ignored issues
–
show
|
|||
| 88 | } catch (\Exception $e) { |
||
| 89 | $htmlText = Craft::t( |
||
| 90 | 'youtubeliveembed', |
||
| 91 | 'Error rendering `{template}` -> {error}', |
||
| 92 | ['template' => $templatePath, 'error' => $e->getMessage()] |
||
| 93 | ); |
||
| 94 | Craft::error($htmlText, __METHOD__); |
||
| 95 | $templateRendered = false; |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | // Restore the old template mode |
||
| 100 | try { |
||
| 101 | Craft::$app->view->setTemplateMode($oldMode); |
||
| 102 | } catch (Exception $e) { |
||
| 103 | Craft::error($e->getMessage(), __METHOD__); |
||
| 104 | } |
||
| 105 | |||
| 106 | return Template::raw($htmlText); |
||
| 107 | } |
||
| 108 | } |
||
| 109 |