-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathaddquicktag.php
402 lines (346 loc) · 9.33 KB
/
addquicktag.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<?php
/**
* Plugin Name: AddQuicktag
* Plugin URI: http://bueltge.de/wp-addquicktags-de-plugin/120/
* Text Domain: addquicktag
* Domain Path: /languages
* Description: Allows you to easily add custom Quicktags to the html- and visual-editor.
* Version: 2.6.1
* Author: Frank Bültge
* Author URI: https://bueltge.de
* License: GPLv3+
* License URI: ./license.txt
*
* Add Quicktag Plugin class
*
* @since 2.0.0
*/
class Add_Quicktag {
/**
* Option key - String
*
* @var string
*/
private static $option_string = 'rmnlQuicktagSettings';
/**
* Use filter 'addquicktag_pages' for add custom pages
*
* @var array
*/
private static $admin_pages_for_js = array(
'post.php',
'post-new.php',
'comment.php',
'edit-comments.php',
'widgets.php',
);
/**
* Use filter 'addquicktag_post_types' for add custom post_types
*
* @var array
*/
private static $post_types_for_js = array( 'comment', 'edit-comments', 'widgets' );
/**
* Store plugin string.
*
* @var string
*/
private static $plugin;
/**
* Handler for the action 'init'. Instantiates this class.
*
* @since 2.0.0
* @access public
* @return \Add_Quicktag $instance
*/
public static function get_object() {
static $instance;
if ( null === $instance ) {
$instance = new self();
}
return $instance;
}
/**
* Constructor, init the functions inside WP
*
* @since 2.0.0
*/
private function __construct() {
if ( ! is_admin() ) {
return;
}
// get string of plugin.
self::$plugin = plugin_basename( __FILE__ );
// on uninstall remove capability from roles.
register_uninstall_hook( __FILE__, array( 'Add_Quicktag', 'uninstall' ) );
// on deactivate delete all settings in database
// register_deactivation_hook( __FILE__, array('Add_Quicktag', 'uninstall' ) );
// load translation files.
add_action( 'admin_init', array( $this, 'localize_plugin' ) );
// on init register post type for addquicktag and print js.
add_action( 'init', array( $this, 'on_admin_init' ) );
add_filter( 'quicktags_settings', array( $this, 'remove_quicktags' ), 10, 1 );
}
/**
* Include other files and print JS
*
* @since 07/16/2012
* @return null|void
*/
public function on_admin_init() {
if ( ! is_admin() ) {
return null;
}
// Include settings.
require_once dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'inc/class-settings.php';
// Include solution for TinyMCE.
require_once dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'inc/class-tinymce.php';
foreach ( $this->get_admin_pages_for_js() as $page ) {
add_action( 'admin_print_scripts-' . $page, array( $this, 'get_json' ) );
add_action( 'admin_print_scripts-' . $page, array( $this, 'admin_enqueue_scripts' ) );
}
}
/**
* Remove quicktags
*
* @since 08/15/2013
*
* @param array $qtags_init the Buttons.
*
* @type string id
* @type array buttons, default: 'strong,em,link,block,del,ins,img,ul,ol,li,code,more,close,fullscreen'
* @return array $qtags_init the Buttons
*/
public function remove_quicktags( $qtags_init ) {
// No core buttons, not necessary to filter.
if ( empty( $qtags_init['buttons'] ) ) {
return $qtags_init;
}
if ( is_multisite() && is_plugin_active_for_network( self::$plugin ) ) {
$options = get_site_option( self::$option_string );
} else {
$options = get_option( self::$option_string );
}
// No settings, not necessary to filter.
if ( empty( $options['core_buttons'] ) ) {
return $qtags_init;
}
// get current screen, post type.
$screen = get_current_screen();
// No information about the backend page, return.
if ( null === $screen->id ) {
return $qtags_init;
}
// Convert string to array from default core buttons.
$buttons = explode( ',', $qtags_init['buttons'] );
// loop about the options to check for each post type.
foreach ( (array) $options['core_buttons'] as $button => $post_type ) {
// if the post type is inside the settings array active, then remove qtags.
if ( is_array( $post_type ) && array_key_exists( $screen->id, $post_type ) ) {
// If settings have key inside, then unset this button.
if ( false !== ( $key = array_search( $button, $buttons, true ) ) ) {
unset( $buttons[ $key ] );
}
}
}
// Convert new buttons array back into a comma-separated string.
$qtags_init['buttons'] = implode( ',', $buttons );
$qtags_init['buttons'] = apply_filters( 'addquicktag_remove_buttons', $qtags_init['buttons'] );
return $qtags_init;
}
/**
* Uninstall data in options table, if the plugin was uninstall via backend
*
* @since 2.0.0
* @return void
*/
public static function uninstall() {
delete_site_option( self::$option_string );
}
/**
* Print json data in head
*
* @since 2.0.0
* @return null|void
*/
public function get_json() {
global $current_screen;
if ( null !== $current_screen->id && ! in_array(
$current_screen->id,
$this->get_post_types_for_js(),
true
) ) {
return null;
}
if ( is_multisite() && is_plugin_active_for_network( $this->get_plugin_string() ) ) {
$options = (array) get_site_option( self::$option_string );
} else {
$options = (array) get_option( self::$option_string );
}
if ( empty( $options['buttons'] ) ) {
$options['buttons'] = array();
}
// allow change or enhance buttons array.
$options['buttons'] = apply_filters( 'addquicktag_buttons', $options['buttons'] );
// hook for filter options.
$options = (array) apply_filters( 'addquicktag_options', $options );
if ( ! $options ) {
return null;
}
if ( 1 < count( $options['buttons'] ) ) {
// sort array by order value.
$tmp = array();
foreach ( (array) $options['buttons'] as $order ) {
if ( isset( $order['order'] ) ) {
$tmp[] = $order['order'];
} else {
$tmp[] = 0;
}
}
array_multisort( $tmp, SORT_ASC, $options['buttons'] );
}
?>
<script type="text/javascript">
var addquicktag_tags = <?php echo wp_json_encode( $options ); ?>,
addquicktag_post_type = <?php echo wp_json_encode( $current_screen->id ); ?>,
addquicktag_pt_for_js = <?php echo wp_json_encode( $this->get_post_types_for_js() ); ?>;
</script>
<?php
}
/**
* Enqueue Scripts for plugin
*
* @internal param string $where
*
* @since 2.0.0
* @access public
* @return null|void
*/
public function admin_enqueue_scripts() {
global $current_screen;
if ( null !== $current_screen->id &&
! in_array(
$current_screen->id,
$this->get_post_types_for_js(),
true
)
) {
return null;
}
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '.dev' : '';
wp_enqueue_script(
$this->get_textdomain() . '_script',
plugins_url( '/js/add-quicktags' . $suffix . '.js', __FILE__ ),
array( 'jquery', 'quicktags' ),
'2021-05-26',
true
);
}
/**
* Localize_plugin function.
*
* @uses load_plugin_textdomain, plugin_basename
* @access public
* @since 2.0.0
* @return void
*/
public function localize_plugin() {
load_plugin_textdomain(
$this->get_textdomain(),
false,
dirname( plugin_basename( __FILE__ ) ) . '/languages'
);
}
/**
* Return plugin comment data.
*
* @since 2.0.0
* @access public
*
* @param string $value default = 'TextDomain'.
* @return string
*/
public function get_plugin_data( $value = 'TextDomain' ) {
static $plugin_data = array();
// fetch the data just once.
if ( isset( $plugin_data[ $value ] ) ) {
return $plugin_data[ $value ];
}
if ( ! function_exists( 'get_plugin_data' ) ) {
// @noinspection
require_once ABSPATH . '/wp-admin/includes/plugin.php';
}
$plugin_data = get_plugin_data( __FILE__ );
return empty( $plugin_data[ $value ] ) ? '' : $plugin_data[ $value ];
}
/**
* Return string of plugin
*
* @since 2.0.0
* @return string
*/
public function get_plugin_string() {
return self::$plugin;
}
/**
* Get Post types with UI to use optional the quicktags
*
* @since 08/1/2013
* @return array
*/
private function get_post_types() {
// list only post types, there was used in UI.
$args = array( 'show_ui' => true );
$post_types = get_post_types( $args );
// simplify the array.
$post_types = array_values( $post_types );
// merge with strings from var.
return array_merge( $post_types, self::$post_types_for_js );
}
/**
* Return allowed post types for include scripts
*
* @since 2.1.1
* @access public
* @return array
*/
public function get_post_types_for_js() {
return apply_filters( 'addquicktag_post_types', $this->get_post_types() );
}
/**
* Return allowed post types for include scripts
*
* @since 2.1.1
* @access public
* @return array
*/
public function get_admin_pages_for_js() {
return apply_filters( 'addquicktag_pages', self::$admin_pages_for_js );
}
/**
* Return textdomain string
*
* @since 2.0.0
* @access public
* @return string
*/
public function get_textdomain() {
return $this->get_plugin_data();
}
/**
* Return string for options
*
* @since 2.0.0
* @return string
*/
public function get_option_string() {
return self::$option_string;
}
} // end class
if ( function_exists( 'add_action' ) && class_exists( 'Add_Quicktag' ) ) {
add_action( 'plugins_loaded', array( 'Add_Quicktag', 'get_object' ) );
} else {
header( 'Status: 403 Forbidden' );
header( 'HTTP/1.1 403 Forbidden' );
exit();
}