-
Notifications
You must be signed in to change notification settings - Fork 5
/
classicpress-editor.php
170 lines (126 loc) · 5.01 KB
/
classicpress-editor.php
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
* -----------------------------------------------------------------------------
* Plugin Name: ClassicPress Editor - Experimental
* Description: An integration of TinyMCE (v5) that brings a modern editing experience to ClassicPress while preserving the familiarity and function that users have grown to love. This plugin is not yet intended for production use.
* Version: 1.0.0
* Author: John Alarcon & ClassicPress Contributors
* Author URI: https://forums.classicpress.net/u/code_potent
* Text Domain: classicpress-editor
* Domain Path: /languages
* -----------------------------------------------------------------------------
* This is free software released under the terms of the General Public License,
* version 2, or later. It is distributed WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Full
* text of the license is available at https://www.gnu.org/licenses/gpl-2.0.txt.
* -----------------------------------------------------------------------------
* Copyright 2021, John Alarcon
* -----------------------------------------------------------------------------
*/
namespace ClassicPress\TinyMce5;
if (!defined('ABSPATH')) {
die();
}
class Editor {
public function __construct() {
// Suppress Tiny in certain contexts.
if ($this->suppress_editor()) {
return;
}
// Disable TinyMCE v4.
add_filter('user_can_richedit', '__return_false');
// Enqueue backend assets.
add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_assets']);
// Enqueue frontend assets.
add_action('wp_enqueue_scripts', [$this, 'enqueue_public_assets']);
// Performant enqueuing for prism.js (for codesample TinyMCE plugin.)
add_filter('the_content', [$this, 'enqueue_prism_assets']);
add_action('admin_print_footer_scripts', [$this, 'window_unload_error_fix']);
}
/**
* Don't load Tiny for comments editor, quick draft editor, other
*/
public function suppress_editor() {
// Not in a relevant view? Bail.
if (!is_admin() || empty($GLOBALS['pagenow'])) {
return false;
}
// Conditions for which to suppress the TinyMCE editor.
if ($GLOBALS['pagenow'] === 'index.php') { // Dashboard quick draft
if ($GLOBALS['plugin_page'] === null) {
return true;
}
} else if ($GLOBALS['pagenow'] === 'comment.php') {
if ($_GET['action'] === 'editcomment') { // Comment edit screen
return true;
}
}
// Do not suppress editor.
return false;
}
public function window_unload_error_fix() {
?>
<script>
jQuery(document).ready(function($) {
// Check screen
if(typeof window.wp.autosave === 'undefined')
return;
// Data Hack
var initialCompareData = {
post_title: $( '#title' ).val() || '',
content: $( '#content' ).val() || '',
excerpt: $( '#excerpt' ).val() || ''
};
var initialCompareString = window.wp.autosave.getCompareString(initialCompareData);
// Fixed postChanged()
window.wp.autosave.server.postChanged = function() {
var changed = false;
// If there are TinyMCE instances, loop through them.
if (window.tinymce) {
window.tinymce.each(['content', 'excerpt'], function(field) {
var editor = window.tinymce.get(field);
if ((editor && editor.isDirty()) || ($('#'+field ).val() || '') !== initialCompareData[field]) {
changed = true;
return false;
}
});
if (($('#title' ).val() || '') !== initialCompareData.post_title) {
changed = true;
}
return changed;
}
return window.wp.autosave.getCompareString() !== initialCompareString;
}
});
</script>
<?php
}
public function enqueue_admin_assets() {
// Enqueue TinyMCE JS.
wp_enqueue_script('classicpress-tinymce5', plugin_dir_url(__FILE__).'scripts/tinymce5/tinymce.min.js');
// Enqueue CP-centric TinyMCE JS.
wp_enqueue_script('classicpress-editor', plugin_dir_url(__FILE__).'scripts/classicpress-editor.js', ['classicpress-tinymce5']);
// Enqueue CP-centric TinyMCE CSS.
wp_enqueue_style('classicpress-editor', plugin_dir_url(__FILE__).'styles/classicpress-editor.css');
}
public function enqueue_public_assets() {
/**
* The prism.js assets are only registered here. They are enqueued later
* on an as-needed basis in the enqueue_prism_assets() method which runs
* on the the_content filter.
*/
wp_register_script('classicpress-editor-syntax-highlighter', plugin_dir_url(__FILE__).'scripts/prism.js', [], time(), true);
wp_register_style('classicpress-editor-syntax-highlighter', plugin_dir_url(__FILE__).'styles/prism.css', [], time());
}
public function enqueue_prism_assets($content) {
// If <pre lang="whatever" is found in $content, enqueue prism assets.
if (preg_match('/<pre *(\w*? *= *["].*?["]|(\w*)*?)>/', $content) === 1) {
wp_enqueue_script('classicpress-editor-syntax-highlighter');
wp_enqueue_style('classicpress-editor-syntax-highlighter');
}
// Return the possibly-amended content.
return $content;
}
}
// Make beautiful all the things.
new Editor;