-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathTinyMCE.php
More file actions
216 lines (192 loc) · 5.33 KB
/
TinyMCE.php
File metadata and controls
216 lines (192 loc) · 5.33 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
/**
* TinyMCE Class.
*
* Necessary operations for classic editor compatibility.
*
* @link https://github.com/googleforcreators/web-stories-wp
*
* @copyright 2021 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
*/
/**
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
declare(strict_types = 1);
namespace Google\Web_Stories\Admin;
use Google\Web_Stories\Assets;
use Google\Web_Stories\Context;
use Google\Web_Stories\Service_Base;
use Google\Web_Stories\Stories_Script_Data;
/**
* Class TinyMCE
*/
class TinyMCE extends Service_Base {
/**
* Web Stories tinymce script handle.
*/
public const SCRIPT_HANDLE = 'web-stories-tinymce-button';
/**
* Assets instance.
*
* @var Assets Assets instance.
*/
private Assets $assets;
/**
* Stories_Script_Data instance.
*
* @var Stories_Script_Data Stories_Script_Data instance.
*/
protected Stories_Script_Data $stories_script_data;
/**
* Context instance.
*
* @var Context Context instance.
*/
private Context $context;
/**
* Tinymce constructor.
*
* @since 1.8.0
*
* @param Assets $assets Assets instance.
* @param Stories_Script_Data $stories_script_data Stories_Script_Data instance.
* @param Context $context Context instance.
*/
public function __construct( Assets $assets, Stories_Script_Data $stories_script_data, Context $context ) {
$this->assets = $assets;
$this->stories_script_data = $stories_script_data;
$this->context = $context;
}
/**
* Initialization actions.
*
* @since 1.5.0
*/
public function register(): void {
if ( $this->context->is_block_editor() || $this->context->is_story_editor() ) {
return;
}
$this->register_assets();
add_action( 'wp_enqueue_editor', [ $this, 'enqueue_assets' ] );
add_filter( 'mce_buttons', [ $this, 'tinymce_web_stories_button' ] );
add_filter( 'mce_external_plugins', [ $this, 'web_stories_mce_plugin' ] );
add_action( 'admin_footer', [ $this, 'web_stories_tinymce_root_element' ] );
add_filter( 'script_loader_tag', [ $this, 'script_loader_tag' ], 10, 3 );
}
/**
* Get the action to use for registering the service.
*
* @since 1.6.0
*
* @return string Registration action to use.
*/
public static function get_registration_action(): string {
return 'admin_enqueue_scripts';
}
/**
* Add web stories button in TinyMCE editor.
*
* @since 1.5.0
*
* @param array|mixed $buttons Array of TinyMCE buttons.
* @return array|mixed
*
* @template T
*
* @phpstan-return ($buttons is array<T> ? array<T> : mixed)
*/
public function tinymce_web_stories_button( $buttons ) {
if ( ! \is_array( $buttons ) ) {
return $buttons;
}
$buttons[] = 'web_stories';
return $buttons;
}
/**
* Register web stories plugin for tinycemce editor.
*
* @since 1.5.0
*
* @param array|mixed $plugins Array of TinyMCE plugin scripts.
* @return array|mixed
*
* @template T
*
* @phpstan-return ($plugins is array<T> ? array<T> : mixed)
*/
public function web_stories_mce_plugin( $plugins ) {
if ( ! \is_array( $plugins ) ) {
return $plugins;
}
$plugins['web_stories'] = $this->assets->get_base_url( \sprintf( 'assets/js/%s.js', self::SCRIPT_HANDLE ) );
return $plugins;
}
/**
* Enqueue related scripts.
*
* @since 1.5.0
*/
public function register_assets(): void {
$this->assets->enqueue_style( 'wp-components' );
$this->assets->enqueue_script_asset( self::SCRIPT_HANDLE );
wp_localize_script(
self::SCRIPT_HANDLE,
'webStoriesData',
$this->stories_script_data->get_script_data()
);
}
/**
* Hijack the button's script to render an empty script tag.
*
* @since 1.5.0
*
* @param string|mixed $tag The `<script>` tag for the enqueued script.
* @param string $handle The script's registered handle.
* @param string $src The script's source URL.
* @return string|mixed The filtered script tag.
*/
public function script_loader_tag( $tag, string $handle, string $src ) {
if ( ! \is_string( $tag ) ) {
return $tag;
}
if ( self::SCRIPT_HANDLE === $handle ) {
$tag = str_replace( $src, '', $tag );
// phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript -- False positive.
$tag = (string) preg_replace( '#<script src=\'\'(.*?)>(.*?)</script>#is', '', $tag );
}
return $tag;
}
/**
* Enqueue related scripts.
*
* @since 1.5.0
*/
public function enqueue_assets(): void {
$this->assets->enqueue_style( 'wp-components' );
$this->assets->enqueue_script_asset( self::SCRIPT_HANDLE );
}
/**
* Root element for tinymce editor.
* This is useful for performing some react operations.
*
* @since 1.5.0
*/
public function web_stories_tinymce_root_element(): void {
?>
<div id="web-stories-tinymce"></div>
<?php
}
}