1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
<?php
namespace TimberEditor;
use Timber\Loader;
use Timber\Post;
use Timber\Timber;
class TimberEditor
{
/**
* TimberEditor constructor.
*/
public function __construct()
{
if (! class_exists(Timber::class)) {
add_action('admin_notices', [$this, 'adminNoticeTimberLibraryMissing']);
return;
}
if (! class_exists(\Classic_Editor::class)) {
add_action('admin_notices', [$this, 'adminNoticeClassicEditor']);
}
$this->run();
}
/**
* admin_notices action callback for missing Timber Library
*/
public function adminNoticeTimberLibraryMissing()
{
?>
<div class="notice notice-error">
<p>
<a href="https://wordpress.org/plugins/timber-library/" target="_blank">Timber</a>
(<a href="<?= admin_url('plugin-install.php?s=timber&tab=search&type=term') ?>">install</a>)
needs to be installed and active.
</p>
</div>
<?php
}
/**
* admin_notices action callback for missing Classic Editor
*/
public function adminNoticeClassicEditor()
{
?>
<div class="notice notice-warning is-dismissible">
<p>
<a href="https://wordpress.org/plugins/classic-editor/" target="_blank">Classic Editor</a>
(<a href="<?= admin_url('plugin-install.php?s=classic+editor&tab=search&type=term') ?>">install</a>)
should be installed and active, because the <b>Gutenberg Editor</b> doesn't play well with <b>CodeMirror</b> <i>currently</i>.
</p>
</div>
<?php
}
/**
* Run the plugin
*/
public function run()
{
if (is_null(Timber::$locations)) {
Timber::$locations = self::getTemplatesLocation();
}
new Settings();
new ThemeEditor();
new MetaBox();
}
/**
* Get the plugins' templates location
* try to use user provided location via Timber::$locations
* or fall back to uploads folder
*
* @return string
*/
public static function getTemplatesLocation()
{
$location = Timber::$locations;
if (is_array($location)) {
$location = $location[array_key_first($location)];
} else if (is_null($location)) {
$location = wp_upload_dir()['basedir'] . '/timber-editor';
}
return apply_filters('TimberEditor/getTemplatesLocation', $location);
}
/**
* Get the current posts' template filename
*
* @param int|null $postId
*
* @return string
*/
public static function getTemplateFilename($postId = null)
{
return apply_filters('TimberEditor/getTemplateFilename', ($postId ?? get_the_ID()) . '.twig', $postId);
}
/**
* Get the current posts' template filepath
*
* @param int|null $postId
*
* @return string
*/
public static function getTemplateFilePath($postId = null)
{
return self::getTemplatesLocation() . '/' . self::getTemplateFilename($postId);
}
/**
* @param array|string $filenames
* @param array $context
* @param bool $expires
* @param string $cacheMode
*
* @return bool|string
*/
public static function render($filenames = [], $context = [], $expires = false, $cacheMode = Loader::CACHE_USE_DEFAULT)
{
$filenames = (array)$filenames;
array_unshift($filenames, self::getTemplateFilename());
return Timber::render($filenames, $context, $expires, $cacheMode);
}
/**
* @param array|string $filenames
* @param array $context
* @param bool $expires
* @param string $cacheMode
*
* @return bool|string
*/
public static function renderPost($filenames = [], $context = [], $expires = false, $cacheMode = Loader::CACHE_USE_DEFAULT)
{
if (! isset($context['post'])) {
$context['post'] = new Post();
}
return self::render($filenames, $context, $expires, $cacheMode);
}
}
|