-
Notifications
You must be signed in to change notification settings - Fork 143
/
class-shortcode-ui.php
438 lines (364 loc) · 12.2 KB
/
class-shortcode-ui.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
<?php
/**
* Primary controller class for Shortcake
*/
class Shortcode_UI {
/**
* Path to the plugin directory.
*
* @access private
* @var string
*/
private $plugin_dir;
/**
* Plugin URL.
*
* @access private
* @var string
*/
private $plugin_url;
/**
* Shortcodes with registered shortcode UI.
*
* @access private
* @var array
*/
private $shortcodes = array();
/**
* Shortcake controller instance.
*
* @access private
* @var object
*/
private static $instance;
/**
* Get instance of Shortcake controller.
*
* Instantiates object on the fly when not already loaded.
*
* @return object
*/
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self;
self::$instance->setup_actions();
}
return self::$instance;
}
/**
* Set up instance attributes when initially loaded.
*/
private function __construct() {
$this->plugin_version = SHORTCODE_UI_VERSION;
$this->plugin_dir = plugin_dir_path( dirname( __FILE__ ) );
$this->plugin_url = plugin_dir_url( dirname( __FILE__ ) );
}
/**
* Setup plugin actions.
*/
private function setup_actions() {
add_action( 'admin_enqueue_scripts', array( $this, 'action_admin_enqueue_scripts' ) );
add_action( 'wp_enqueue_editor', array( $this, 'action_wp_enqueue_editor' ) );
add_action( 'wp_ajax_bulk_do_shortcode', array( $this, 'handle_ajax_bulk_do_shortcode' ) );
add_filter( 'wp_editor_settings', array( $this, 'filter_wp_editor_settings' ), 10, 2 );
}
/**
* When a WP_Editor is initialized on a page, call the 'register_shortcode_ui' action.
*
* This action can be used to register styles and shortcode UI for any
* shortcake-powered shortcodes, only on views which actually include a WP
* Editor.
*/
public function filter_wp_editor_settings( $settings, $editor_id ) {
if ( ! did_action( 'register_shortcode_ui' ) ) {
/**
* Register shortcode UI for shortcodes.
*
* Can be used to register shortcode UI only when an editor is being enqueued.
*
* @param array $settings Settings array for the ective WP_Editor.
*/
do_action( 'register_shortcode_ui', $settings, $editor_id );
}
return $settings;
}
/**
* Register UI for a shortcode.
*
* @param string $shortcode_tag Tag used for the shortcode.
* @param array $args Configuration parameters for shortcode UI.
*/
public function register_shortcode_ui( $shortcode_tag, $args = array() ) {
// inner_content=true is a valid argument, but we want more detail
if ( isset( $args['inner_content'] ) && true === $args['inner_content'] ) {
$args['inner_content'] = array(
'label' => esc_html__( 'Inner Content', 'shortcode-ui' ),
'description' => '',
);
}
if ( ! isset( $args['attrs'] ) ) {
$args['attrs'] = array();
}
$args['shortcode_tag'] = $shortcode_tag;
$this->shortcodes[ $shortcode_tag ] = $args;
// Setup filter to handle decoding encoded attributes.
add_filter( "shortcode_atts_{$shortcode_tag}", array( $this, 'filter_shortcode_atts_decode_encoded' ), 5, 3 );
}
/**
* Get configuration parameters for all shortcodes with UI.
*
* @return array
*/
public function get_shortcodes() {
if ( ! did_action( 'register_shortcode_ui' ) ) {
/**
* Register shortcode UI for shortcodes.
*
* Can be used to register shortcode UI only when an editor is being enqueued.
*
* @param array $settings Settings array for the ective WP_Editor.
*/
do_action( 'register_shortcode_ui', array(), '' );
}
/**
* Filter the returned shortcode UI configuration parameters.
*
* Used to remove shortcode UI that's already been registered.
*
* @param array $shortcodes
*/
$shortcodes = apply_filters( 'shortcode_ui_shortcodes', $this->shortcodes );
foreach ( $shortcodes as $shortcode => $args ) {
foreach ( $args['attrs'] as $key => $value ) {
foreach ( array( 'label', 'description' ) as $field ) {
if ( ! empty( $value[ $field ] ) ) {
$shortcodes[ $shortcode ]['attrs'][ $key ][ $field ] = wp_kses_post( $value[ $field ] );
}
}
}
foreach ( array( 'label', 'description' ) as $field ) {
if ( ! empty( $args['inner_content'][ $field ] ) ) {
$shortcodes[ $shortcode ]['inner_content'][ $field ] = wp_kses_post( $args['inner_content'][ $field ] );
}
}
}
return $shortcodes;
}
/**
* Get UI configuration parameters for a given shortcode.
*
* @return array|false
*/
public function get_shortcode( $shortcode_tag ) {
$shortcodes = $this->get_shortcodes();
if ( isset( $shortcodes[ $shortcode_tag ] ) ) {
return $shortcodes[ $shortcode_tag ];
}
return false;
}
/**
* Enqueue scripts and styles used in the admin.
*
* Editor styles needs to be added before wp_enqueue_editor.
*
* @param array $editor_supports Whether or not the editor being enqueued has 'tinymce' or 'quicktags'
*/
public function action_admin_enqueue_scripts( $editor_supports ) {
add_editor_style( trailingslashit( $this->plugin_url ) . 'css/shortcode-ui-editor-styles.css' );
}
/**
* Enqueue scripts and styles needed for shortcode UI.
*/
public function enqueue() {
if ( did_action( 'enqueue_shortcode_ui' ) ) {
return;
}
wp_enqueue_media();
$shortcodes = array_values( $this->get_shortcodes() );
$current_post_type = get_post_type();
if ( $current_post_type ) {
foreach ( $shortcodes as $key => $args ) {
if ( ! empty( $args['post_type'] ) && ! in_array( $current_post_type, $args['post_type'] ) ) {
unset( $shortcodes[ $key ] );
}
}
}
if ( empty( $shortcodes ) ) {
return;
}
usort( $shortcodes, array( $this, 'compare_shortcodes_by_label' ) );
// Load minified version of wp-js-hooks if not debugging.
$wp_js_hooks_file = 'wp-js-hooks' . ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '.min' : '' ) . '.js';
wp_enqueue_script( 'shortcode-ui-js-hooks', $this->plugin_url . 'lib/wp-js-hooks/' . $wp_js_hooks_file, array(), '2015-03-19' );
wp_enqueue_script( 'shortcode-ui', $this->plugin_url . 'js/build/shortcode-ui.js', array( 'jquery', 'backbone', 'mce-view', 'shortcode-ui-js-hooks' ), $this->plugin_version );
wp_enqueue_style( 'shortcode-ui', $this->plugin_url . 'css/shortcode-ui.css', array(), $this->plugin_version );
wp_localize_script( 'shortcode-ui', ' shortcodeUIData', array(
'shortcodes' => $shortcodes,
'strings' => array(
'media_frame_title' => __( 'Insert Post Element', 'shortcode-ui' ),
'media_frame_menu_insert_label' => __( 'Insert Post Element', 'shortcode-ui' ),
'media_frame_menu_update_label' => __( '%s Details', 'shortcode-ui' ), // Substituted in JS
'media_frame_toolbar_insert_label' => __( 'Insert Element', 'shortcode-ui' ),
'media_frame_toolbar_update_label' => __( 'Update', 'shortcode-ui' ),
'media_frame_no_attributes_message' => __( 'There are no attributes to configure for this Post Element.', 'shortcode-ui' ),
'mce_view_error' => __( 'Failed to load preview', 'shortcode-ui' ),
'search_placeholder' => __( 'Search', 'shortcode-ui' ),
'insert_content_label' => __( 'Insert Content', 'shortcode-ui' ),
),
'nonces' => array(
'preview' => wp_create_nonce( 'shortcode-ui-preview' ),
'thumbnailImage' => wp_create_nonce( 'shortcode-ui-get-thumbnail-image' ),
),
) );
// add templates to the footer, instead of where we're at now
add_action( 'admin_print_footer_scripts', array( $this, 'action_admin_print_footer_scripts' ) );
/**
* Fires after shortcode UI assets have been enqueued.
*
* Will only fire once per page load.
*/
do_action( 'enqueue_shortcode_ui' );
}
/**
* Enqueue shortcode UI assets when the editor is enqueued.
*/
public function action_wp_enqueue_editor() {
$this->enqueue();
/**
* Fires after shortcode UI assets have been loaded for the editor.
*
* Will fire every time the editor is loaded.
*/
do_action( 'shortcode_ui_loaded_editor' );
}
/**
* Output required underscore.js templates in the footer
*/
public function action_admin_print_footer_scripts() {
echo $this->get_view( 'media-frame' ); // WPCS: xss ok
echo $this->get_view( 'list-item' ); // WPCS: xss ok
echo $this->get_view( 'edit-form' ); // WPCS: xss ok
/**
* Fires after base shortcode UI templates have been loaded.
*
* Allows custom shortcode UI field types to load their own templates.
*/
do_action( 'print_shortcode_ui_templates' );
}
/**
* Helper function for displaying a PHP template file.
*
* Template args array is extracted and passed to the template file.
*
* @param string $template full template file path. Or name of template file in inc/templates.
* @return string the template contents
*/
public function get_view( $template ) {
if ( ! file_exists( $template ) ) {
$template_dir = $this->plugin_dir . 'inc/templates/';
$template = $template_dir . $template . '.tpl.php';
if ( ! file_exists( $template ) ) {
return '';
}
}
ob_start();
include $template;
return ob_get_clean();
}
/**
* Sort labels alphabetically.
*
* @param array $a
* @param array $b
* @return int
*/
private function compare_shortcodes_by_label( $a, $b ) {
return strcmp( $a['label'], $b['label'] );
}
/**
* Render a shortcode body for preview.
*/
private function render_shortcode_for_preview( $shortcode, $post_id = null ) {
if ( ! defined( 'SHORTCODE_UI_DOING_PREVIEW' ) ) {
define( 'SHORTCODE_UI_DOING_PREVIEW', true );
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return esc_html__( "Something's rotten in the state of Denmark", 'shortcode-ui' );
}
if ( ! empty( $post_id ) ) {
// @codingStandardsIgnoreStart
global $post;
$post = get_post( $post_id );
setup_postdata( $post );
// @codingStandardsIgnoreEnd
}
ob_start();
/**
* Fires before shortcode is rendered in preview.
*
* @param string $shortcode Full shortcode including attributes
*/
do_action( 'shortcode_ui_before_do_shortcode', $shortcode );
echo do_shortcode( $shortcode ); // WPCS: xss ok
/**
* Fires after shortcode is rendered in preview.
*
* @param string $shortcode Full shortcode including attributes
*/
do_action( 'shortcode_ui_after_do_shortcode', $shortcode );
return ob_get_clean();
}
/**
* Get a bunch of shortcodes to render in MCE preview.
*/
public function handle_ajax_bulk_do_shortcode() {
if ( is_array( $_POST['queries'] ) ) {
$responses = array();
foreach ( $_POST['queries'] as $posted_query ) {
// Don't sanitize shortcodes — can contain HTML kses doesn't allow (e.g. sourcecode shortcode)
if ( ! empty( $posted_query['shortcode'] ) ) {
$shortcode = stripslashes( $posted_query['shortcode'] );
} else {
$shortcode = null;
}
if ( isset( $posted_query['post_id'] ) ) {
$post_id = intval( $posted_query['post_id'] );
} else {
$post_id = null;
}
$responses[ $posted_query['counter'] ] = array(
'query' => $posted_query,
'response' => $this->render_shortcode_for_preview( $shortcode, $post_id ),
);
}
wp_send_json_success( $responses );
exit;
}
}
/**
* Decode any encoded attributes.
*
* @param array $out The output array of shortcode attributes.
* @param array $pairs The supported attributes and their defaults.
* @param array $atts The user defined shortcode attributes.
* @return array $out The output array of shortcode attributes.
*/
public function filter_shortcode_atts_decode_encoded( $out, $pairs, $atts ) {
// Get current shortcode tag from the current filter
// by stripping `shortcode_atts_` from start of string.
$shortcode_tag = substr( current_filter(), 15 );
if ( ! isset( $this->shortcodes[ $shortcode_tag ] ) ) {
return $out;
}
$fields = Shortcode_UI_Fields::get_instance()->get_fields();
$args = $this->shortcodes[ $shortcode_tag ];
foreach ( $args['attrs'] as $attr ) {
$default = isset( $fields[ $attr['type'] ]['encode'] ) ? $fields[ $attr['type'] ]['encode'] : false;
$encoded = isset( $attr['encode'] ) ? $attr['encode'] : $default;
if ( $encoded && isset( $out[ $attr['attr'] ] ) ) {
$out[ $attr['attr'] ] = rawurldecode( $out[ $attr['attr'] ] );
}
}
return $out;
}
}