|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Black Studio TinyMCE Widget - Admin assets |
|
4
|
|
|
* |
|
5
|
|
|
* @package Black_Studio_TinyMCE_Widget |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Black_Studio_TinyMCE_Widget\Admin; |
|
9
|
|
|
|
|
10
|
|
|
use WP_Query; |
|
11
|
|
|
|
|
12
|
|
|
// Exit if accessed directly. |
|
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
14
|
|
|
exit; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
if ( ! class_exists( 'Black_Studio_TinyMCE_Widget\\Admin\\Admin_Assets', false ) ) { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class that provides admin functionalities |
|
21
|
|
|
* |
|
22
|
|
|
* @package Black_Studio_TinyMCE_Widget |
|
23
|
|
|
* @since 3.0.0 |
|
24
|
|
|
*/ |
|
25
|
|
|
final class Admin_Assets { |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The single instance of the class |
|
29
|
|
|
* |
|
30
|
|
|
* @var object |
|
31
|
|
|
* @since 3.0.0 |
|
32
|
|
|
*/ |
|
33
|
|
|
protected static $_instance = null; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Return the single class instance |
|
37
|
|
|
* |
|
38
|
|
|
* @return object |
|
39
|
|
|
* @since 3.0.0 |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function instance() { |
|
42
|
|
|
if ( is_null( self::$_instance ) ) { |
|
43
|
|
|
self::$_instance = new self(); |
|
44
|
|
|
} |
|
45
|
|
|
return self::$_instance; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Class constructor |
|
50
|
|
|
* |
|
51
|
|
|
* @uses add_action() |
|
52
|
|
|
* |
|
53
|
|
|
* @since 3.0.0 |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function __construct() { |
|
56
|
|
|
// Register action and filter hooks. |
|
57
|
|
|
add_action( 'admin_init', array( $this, 'admin_init' ), 20 ); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Prevent the class from being cloned |
|
62
|
|
|
* |
|
63
|
|
|
* @return void |
|
64
|
|
|
* @since 3.0.0 |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function __clone() { |
|
67
|
|
|
_doing_it_wrong( __FUNCTION__, esc_html__( 'Cheatin’ uh?' ), '2.0' ); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Add actions and filters (only in widgets admin page) |
|
72
|
|
|
* |
|
73
|
|
|
* @uses add_action() |
|
74
|
|
|
* @uses add_filter() |
|
75
|
|
|
* |
|
76
|
|
|
* @return void |
|
77
|
|
|
* @since 2.0.0 |
|
78
|
|
|
*/ |
|
79
|
|
|
public function admin_init() { |
|
80
|
|
|
if ( bstw()->admin()->enabled() ) { |
|
81
|
|
|
add_action( 'admin_head', array( $this, 'enqueue_media' ) ); |
|
82
|
|
|
add_action( 'admin_print_scripts', array( $this, 'admin_print_scripts' ) ); |
|
83
|
|
|
add_action( 'admin_print_styles', array( $this, 'admin_print_styles' ) ); |
|
84
|
|
|
add_action( 'admin_print_footer_scripts', array( $this, 'admin_print_footer_scripts' ) ); |
|
85
|
|
|
add_action( 'wp_tiny_mce_init', array( $this, 'wp_tiny_mce_init' ) ); |
|
86
|
|
|
add_filter( 'wp_editor_settings', array( $this, 'editor_settings' ), 5, 2 ); |
|
87
|
|
|
add_filter( 'tiny_mce_before_init', array( $this, 'tinymce_fix_rtl' ), 10 ); |
|
88
|
|
|
add_filter( 'tiny_mce_before_init', array( $this, 'tinymce_fullscreen' ), 10, 2 ); |
|
89
|
|
|
add_filter( 'quicktags_settings', array( $this, 'quicktags_fullscreen' ), 10, 2 ); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Instantiate tinyMCE editor |
|
95
|
|
|
* |
|
96
|
|
|
* @uses add_thickbox() |
|
97
|
|
|
* @uses wp_enqueue_media() |
|
98
|
|
|
* |
|
99
|
|
|
* @return void |
|
100
|
|
|
* @since 3.0.0 |
|
101
|
|
|
*/ |
|
102
|
|
|
public function enqueue_media() { |
|
103
|
|
|
// Add support for thickbox media dialog. |
|
104
|
|
|
add_thickbox(); |
|
105
|
|
|
// New media modal dialog (WP 3.5+). |
|
106
|
|
|
if ( function_exists( 'wp_enqueue_media' ) ) { |
|
107
|
|
|
wp_enqueue_media(); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Enqueue styles |
|
113
|
|
|
* |
|
114
|
|
|
* @uses wp_enqueue_style() |
|
115
|
|
|
* |
|
116
|
|
|
* @return void |
|
117
|
|
|
* @since 3.0.0 |
|
118
|
|
|
*/ |
|
119
|
|
|
public function admin_print_styles() { |
|
120
|
|
|
wp_enqueue_style( 'wp-jquery-ui-dialog' ); |
|
121
|
|
|
wp_enqueue_style( 'editor-buttons' ); |
|
122
|
|
|
$this->enqueue_style(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Helper function to enqueue style |
|
127
|
|
|
* |
|
128
|
|
|
* @uses apply_filters() |
|
129
|
|
|
* @uses wp_enqueue_style() |
|
130
|
|
|
* @uses plugins_url() |
|
131
|
|
|
* @uses SCRIPT_DEBUG |
|
132
|
|
|
* |
|
133
|
|
|
* @return void |
|
134
|
|
|
* @since 3.0.0 |
|
135
|
|
|
*/ |
|
136
|
|
|
public function enqueue_style() { |
|
137
|
|
|
$style = apply_filters( 'black_studio_tinymce_widget_style', 'black-studio-tinymce-widget' ); |
|
138
|
|
|
$path = apply_filters( 'black_studio_tinymce_widget_style_path', 'css/' ); |
|
139
|
|
|
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '.css' : '.min.css'; |
|
140
|
|
|
wp_enqueue_style( |
|
141
|
|
|
$style, |
|
142
|
|
|
plugins_url( $path . $style . $suffix, dirname( dirname( __FILE__ ) ) ), |
|
143
|
|
|
array(), |
|
144
|
|
|
bstw()->get_version() |
|
145
|
|
|
); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Enqueue header scripts |
|
150
|
|
|
* |
|
151
|
|
|
* @uses wp_enqueue_script() |
|
152
|
|
|
* @uses do_action() |
|
153
|
|
|
* |
|
154
|
|
|
* @return void |
|
155
|
|
|
* @since 3.0.0 |
|
156
|
|
|
*/ |
|
157
|
|
|
public function admin_print_scripts() { |
|
158
|
|
|
wp_enqueue_script( 'media-upload' ); |
|
159
|
|
|
wp_enqueue_script( 'wplink' ); |
|
160
|
|
|
wp_enqueue_script( 'wpdialogs-popup' ); |
|
161
|
|
|
$this->enqueue_script(); |
|
162
|
|
|
$this->localize_script(); |
|
163
|
|
|
do_action( 'wp_enqueue_editor', array( 'tinymce' => true ) ); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Helper function to enqueue script |
|
168
|
|
|
* |
|
169
|
|
|
* @uses apply_filters() |
|
170
|
|
|
* @uses wp_enqueue_script() |
|
171
|
|
|
* @uses plugins_url() |
|
172
|
|
|
* @uses SCRIPT_DEBUG |
|
173
|
|
|
* |
|
174
|
|
|
* @return void |
|
175
|
|
|
* @since 3.0.0 |
|
176
|
|
|
*/ |
|
177
|
|
|
public function enqueue_script() { |
|
178
|
|
|
$script = apply_filters( 'black_studio_tinymce_widget_script', 'black-studio-tinymce-widget' ); |
|
179
|
|
|
$path = apply_filters( 'black_studio_tinymce_widget_script_path', 'js/' ); |
|
180
|
|
|
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '.js' : '.min.js'; |
|
181
|
|
|
wp_enqueue_script( |
|
182
|
|
|
$script, |
|
183
|
|
|
plugins_url( $path . $script . $suffix, dirname( dirname( __FILE__ ) ) ), |
|
184
|
|
|
array( 'jquery', 'editor', 'quicktags' ), |
|
185
|
|
|
bstw()->get_version(), |
|
186
|
|
|
true |
|
187
|
|
|
); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Helper function to enqueue localized script |
|
192
|
|
|
* |
|
193
|
|
|
* @uses apply_filters() |
|
194
|
|
|
* @uses wp_localize_script() |
|
195
|
|
|
* |
|
196
|
|
|
* @return void |
|
197
|
|
|
* @since 3.0.0 |
|
198
|
|
|
*/ |
|
199
|
|
|
public function localize_script() { |
|
200
|
|
|
$container_selectors = apply_filters( 'black_studio_tinymce_container_selectors', array( 'div.widget', 'div.widget-inside' ) ); |
|
201
|
|
|
$activate_events = apply_filters( 'black_studio_tinymce_activate_events', array() ); |
|
202
|
|
|
$deactivate_events = apply_filters( 'black_studio_tinymce_deactivate_events', array() ); |
|
203
|
|
|
$data = array( |
|
204
|
|
|
'dummy_post_id' => bstw()->admin()->get_dummy_post_id(), |
|
205
|
|
|
'container_selectors' => implode( ', ', $container_selectors ), |
|
206
|
|
|
'activate_events' => $activate_events, |
|
207
|
|
|
'deactivate_events' => $deactivate_events, |
|
208
|
|
|
/* translators: error message shown when a duplicated widget ID is detected */ |
|
209
|
|
|
'error_duplicate_id' => __( 'ERROR: Duplicate widget ID detected. To avoid content loss, please create a new widget with the same content and then delete this one.', 'black-studio-tinymce-widget' ), |
|
210
|
|
|
); |
|
211
|
|
|
wp_localize_script( apply_filters( 'black_studio_tinymce_widget_script', 'black-studio-tinymce-widget' ), 'bstwData', $data ); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Enqueue footer scripts |
|
216
|
|
|
* |
|
217
|
|
|
* @return void |
|
218
|
|
|
* @since 3.0.0 |
|
219
|
|
|
*/ |
|
220
|
|
|
public function admin_print_footer_scripts() { |
|
221
|
|
|
bstw()->admin()->editor( '', 'black-studio-tinymce-widget', 'black-studio-tinymce-widget' ); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Setup editor instance for event handling |
|
226
|
|
|
* |
|
227
|
|
|
* @uses SCRIPT_DEBUG |
|
228
|
|
|
* |
|
229
|
|
|
* @return void |
|
230
|
|
|
* @since 3.0.0 |
|
231
|
|
|
*/ |
|
232
|
|
|
public function wp_tiny_mce_init() { |
|
233
|
|
|
$script = 'black-studio-tinymce-widget-setup'; |
|
234
|
|
|
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '.js' : '.min.js'; |
|
235
|
|
|
// phpcs:ignore WordPress.WP.EnqueuedResources |
|
236
|
|
|
echo '<script type="text/javascript" src="' . plugins_url( 'js/' . $script . $suffix, dirname( dirname( __FILE__ ) ) ) . '"></script>' . "\n"; // xss ok. |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Set editor settings |
|
241
|
|
|
* |
|
242
|
|
|
* @param mixed[] $settings Array of settings. |
|
243
|
|
|
* @param string $editor_id Editor instance ID. |
|
244
|
|
|
* @return mixed[] |
|
245
|
|
|
* @since 3.0.0 |
|
246
|
|
|
*/ |
|
247
|
|
|
public function editor_settings( $settings, $editor_id ) { |
|
248
|
|
|
if ( strstr( $editor_id, 'black-studio-tinymce' ) ) { |
|
249
|
|
|
$settings['tinymce'] = array( |
|
250
|
|
|
'wp_skip_init' => 'widget-black-studio-tinymce-__i__-text' === $editor_id, |
|
251
|
|
|
'add_unload_trigger' => false, |
|
252
|
|
|
'wp_autoresize_on' => false, |
|
253
|
|
|
); |
|
254
|
|
|
$settings['editor_height'] = 350; |
|
255
|
|
|
$settings['dfw'] = true; |
|
256
|
|
|
$settings['editor_class'] = 'black-studio-tinymce'; |
|
257
|
|
|
} |
|
258
|
|
|
return $settings; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Fix for rtl languages |
|
263
|
|
|
* |
|
264
|
|
|
* @param mixed[] $settings Array of settings. |
|
265
|
|
|
* @return mixed[] |
|
266
|
|
|
* @since 3.0.0 |
|
267
|
|
|
*/ |
|
268
|
|
|
public function tinymce_fix_rtl( $settings ) { |
|
269
|
|
|
// This fix has to be applied to all editor instances (not just BSTW ones). |
|
270
|
|
|
if ( is_rtl() && isset( $settings['plugins'] ) && ',directionality' === $settings['plugins'] ) { |
|
271
|
|
|
unset( $settings['plugins'] ); |
|
272
|
|
|
} |
|
273
|
|
|
return $settings; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Apply TinyMCE default fullscreen |
|
278
|
|
|
* |
|
279
|
|
|
* @param mixed[] $settings Array of settings. |
|
280
|
|
|
* @param string|null $editor_id Editor ID. |
|
281
|
|
|
* @return mixed[] |
|
282
|
|
|
* @since 3.0.0 |
|
283
|
|
|
*/ |
|
284
|
|
|
public function tinymce_fullscreen( $settings, $editor_id = null ) { |
|
285
|
|
|
if ( strstr( $editor_id, 'black-studio-tinymce' ) ) { |
|
286
|
|
|
for ( $i = 1; $i <= 4; $i++ ) { |
|
287
|
|
|
$toolbar = 'toolbar' . $i; |
|
288
|
|
|
if ( isset( $settings[ $toolbar ] ) ) { |
|
289
|
|
|
$settings[ $toolbar ] = str_replace( 'wp_fullscreen', 'wp_fullscreen,fullscreen', $settings[ $toolbar ] ); |
|
290
|
|
|
} |
|
291
|
|
|
} |
|
292
|
|
|
} |
|
293
|
|
|
return $settings; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* Disable Quicktags default fullscreen |
|
298
|
|
|
* |
|
299
|
|
|
* @param mixed[] $settings Array of settings. |
|
300
|
|
|
* @param string $editor_id Editor ID. |
|
301
|
|
|
* @return mixed[] |
|
302
|
|
|
* @since 3.0.0 |
|
303
|
|
|
*/ |
|
304
|
|
|
public function quicktags_fullscreen( $settings, $editor_id ) { |
|
305
|
|
|
if ( strstr( $editor_id, 'black-studio-tinymce' ) ) { |
|
306
|
|
|
$settings['buttons'] = str_replace( ',fullscreen', '', $settings['buttons'] ); |
|
307
|
|
|
} |
|
308
|
|
|
return $settings; |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
} // END class |
|
312
|
|
|
|
|
313
|
|
|
} // END class_exists check |
|
314
|
|
|
|