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