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\models; |
13
|
|
|
|
14
|
|
|
use Craft; |
15
|
|
|
use nystudio107\codeeditor\validators\TwigTemplateValidator; |
16
|
|
|
use nystudio107\seomatic\base\FrontendTemplate; |
17
|
|
|
use nystudio107\seomatic\helpers\PluginTemplate as PluginTemplateHelper; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author nystudio107 |
21
|
|
|
* @package Seomatic |
22
|
|
|
* @since 3.0.0 |
23
|
|
|
*/ |
24
|
|
|
class EditableTemplate extends FrontendTemplate |
25
|
|
|
{ |
26
|
|
|
// Constants |
27
|
|
|
// ========================================================================= |
28
|
|
|
|
29
|
|
|
public const TEMPLATE_TYPE = 'EditableTemplate'; |
30
|
|
|
|
31
|
|
|
// Static Methods |
32
|
|
|
// ========================================================================= |
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
* @deprecated This is no longer used |
36
|
|
|
*/ |
37
|
|
|
public $templateVersion = '1.0.0'; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
// Public Properties |
41
|
|
|
// ========================================================================= |
42
|
|
|
/** |
43
|
|
|
* The template to render this FrontendTemplate |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
public $templateString; |
48
|
|
|
/** |
49
|
|
|
* @var int |
50
|
|
|
*/ |
51
|
|
|
public $siteId; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param array $config |
55
|
|
|
* |
56
|
|
|
* @return null|EditableTemplate |
57
|
|
|
*/ |
58
|
|
|
public static function create(array $config = []) |
59
|
|
|
{ |
60
|
|
|
$model = new EditableTemplate($config); |
61
|
|
|
// Load $templateString from the source template if it's not set |
62
|
|
|
if (empty($model->templateString)) { |
63
|
|
|
$model->loadTemplate(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $model; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Public Methods |
70
|
|
|
// ========================================================================= |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Load the existing template into a string |
74
|
|
|
*/ |
75
|
|
|
public function loadTemplate() |
76
|
|
|
{ |
77
|
|
|
$this->templateString = ''; |
78
|
|
|
// First try it from the Craft template directory |
79
|
|
|
$path = Craft::getAlias('@templates/') |
|
|
|
|
80
|
|
|
. $this->template; |
81
|
|
|
if (file_exists($path)) { |
82
|
|
|
$this->templateString = @file_get_contents($path); |
|
|
|
|
83
|
|
|
} else { |
84
|
|
|
// Next try from our plugin directory first |
85
|
|
|
$path = Craft::getAlias('@nystudio107/seomatic/templates/') |
|
|
|
|
86
|
|
|
. $this->template; |
87
|
|
|
if (file_exists($path)) { |
88
|
|
|
$this->templateString = @file_get_contents($path); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritdoc |
95
|
|
|
*/ |
96
|
|
|
public function rules(): array |
97
|
|
|
{ |
98
|
|
|
$rules = parent::rules(); |
99
|
|
|
$rules = array_merge($rules, [ |
100
|
|
|
[['templateString'], 'required'], |
101
|
|
|
[['templateString'], 'string'], |
102
|
|
|
[['templateString'], TwigTemplateValidator::class], |
103
|
|
|
]); |
104
|
|
|
|
105
|
|
|
return $rules; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @inheritdoc |
110
|
|
|
*/ |
111
|
|
|
public function render(array $params = []): string |
112
|
|
|
{ |
113
|
|
|
return PluginTemplateHelper::renderStringTemplate($this->templateString); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|