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
|
<?php
namespace TimberEditor;
class MetaBox
{
/**
* Metabox constructor.
*/
public function __construct()
{
add_action('add_meta_boxes', [$this, 'addMetaBoxes']);
foreach (Settings::getGeneralSupportedPostTypes() as $postType) {
add_action("save_post_{$postType}", [$this, 'savePost']);
}
}
/**
* add_meta_boxes action callback
*
* @param $postType
*/
public function addMetaBoxes($postType)
{
if (! in_array($postType, Settings::getGeneralSupportedPostTypes())) {
return;
}
add_meta_box('timber-editor', 'Timber Editor', [$this, 'metaBoxTimberEditor'], '', 'advanced', 'high');
}
/**
* save_post action callback
* Writes the content to file
*
* @param $postId
*/
public function savePost($postId)
{
if (
(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) ||
(! isset($_POST['post_ID']) || $_POST['post_ID'] != $postId) ||
! check_admin_referer('metaBoxTimberEditor', 'metaBoxTimberEditor') ||
! isset($_POST['timber-editor_content'])
) {
return;
}
$content = wp_kses($_POST['timber-editor_content'], wp_kses_allowed_html('post'));
file_put_contents(TimberEditor::getTemplateFilePath($postId), $content);
if (empty($_POST['timber-editor_content'])) {
wp_delete_file(TimberEditor::getTemplateFilePath($postId));
}
}
/**
* add_meta_box callback
*/
public function metaBoxTimberEditor()
{
$file = TimberEditor::getTemplateFilePath();
if (file_exists($file)) {
$f = fopen($file, 'r');
$content = fread($f, filesize($file));
fclose($f);
}
wp_nonce_field('metaBoxTimberEditor', 'metaBoxTimberEditor');
?>
<textarea name="timber-editor_content" id="timber-editor_content"><?= esc_textarea($content ?? '') ?></textarea>
<?php
$settings = wp_enqueue_code_editor([
'file' => $file,
'codemirror' => [
'theme' => Settings::getCodeMirrorTheme(),
],
]);
wp_add_inline_script('code-editor', sprintf('jQuery( function() { wp.codeEditor.initialize( "timber-editor_content", %s ); } );', wp_json_encode($settings)));
}
}
|