-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathclass-front-end.php
458 lines (389 loc) · 12.1 KB
/
class-front-end.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
<?php
namespace Code_Snippets;
use WP_Post;
use WP_REST_Response;
use WP_REST_Server;
/**
* This class manages the shortcodes included with the plugin,
*
* @package Code_Snippets
*/
class Front_End {
/**
* Name of the shortcode tag for rendering the code source
*/
const SOURCE_SHORTCODE = 'code_snippet_source';
/**
* Name of the shortcode tag for rendering content snippets
*/
const CONTENT_SHORTCODE = 'code_snippet';
/**
* Handle to use for front-end scripts and styles.
*/
const PRISM_HANDLE = 'code-snippets-prism';
/**
* Maximum depth for shortcode recursion.
*/
const MAX_SHORTCODE_DEPTH = 5;
/**
* Class constructor
*/
public function __construct() {
add_action( 'the_posts', [ $this, 'enqueue_highlighting' ] );
add_action( 'init', [ $this, 'setup_mce_plugin' ] );
add_shortcode( self::CONTENT_SHORTCODE, [ $this, 'render_content_shortcode' ] );
add_shortcode( self::SOURCE_SHORTCODE, [ $this, 'render_source_shortcode' ] );
add_filter( 'code_snippets/render_content_shortcode', 'trim' );
}
/**
* Register REST API routes for use in front-end plugins.
*
* @return void
*/
public function register_rest_routes() {
register_rest_route(
'v1/snippets',
'/snippets-info',
array(
'methods' => WP_REST_Server::READABLE,
'callback' => [ $this, 'get_snippets_info' ],
'permission_callback' => function () {
return current_user_can( 'edit_posts' );
},
)
);
}
/**
* Fetch snippets data in response to a request.
*
* @return WP_REST_Response
*/
public function get_snippets_info(): WP_REST_Response {
$snippets = get_snippets();
$data = [];
foreach ( $snippets as $snippet ) {
$data[] = [
'id' => $snippet->id,
'name' => $snippet->name,
'type' => $snippet->type,
'active' => $snippet->active,
];
}
return new WP_REST_Response( $data, 200 );
}
/**
* Perform the necessary actions to add a button to the TinyMCE editor
*/
public function setup_mce_plugin() {
if ( ! code_snippets()->current_user_can() ) {
return;
}
/* Register the TinyMCE plugin */
add_filter(
'mce_external_plugins',
function ( $plugins ) {
$plugins['code_snippets'] = plugins_url( 'dist/mce.js', PLUGIN_FILE );
return $plugins;
}
);
/* Add the button to the editor toolbar */
add_filter(
'mce_buttons',
function ( $buttons ) {
$buttons[] = 'code_snippets';
return $buttons;
}
);
/* Add the translation strings to the TinyMCE editor */
add_filter(
'mce_external_languages',
function ( $languages ) {
$languages['code_snippets'] = __DIR__ . '/mce-strings.php';
return $languages;
}
);
}
/**
* Enqueue the syntax highlighting assets if they are required for the current posts
*
* @param array<WP_Post|int>|null|false $posts List of currently visible posts.
*
* @return array<WP_Post|int>|null|false Unchanged list of posts.
*/
public function enqueue_highlighting( $posts ) {
// Exit early if there are no posts to check or if the highlighter has been disabled.
if ( empty( $posts ) || Settings\get_setting( 'general', 'disable_prism' ) ) {
return $posts;
}
// Loop through the posts, checking for an existing shortcode, short-circuiting if possible.
$found_shortcode_content = null;
foreach ( $posts as $post ) {
if ( false !== stripos( $post->post_content, '[' . self::SOURCE_SHORTCODE ) ||
false !== strpos( $post->post_content, '<!-- wp:code-snippets/source ' ) ) {
$found_shortcode_content = $post->post_content;
break;
}
}
// Load assets on the appropriate hook if a matching shortcode was found.
if ( null !== $found_shortcode_content ) {
$this->register_prism_assets();
add_action(
'wp_enqueue_scripts',
function () {
wp_enqueue_style( self::PRISM_HANDLE );
wp_enqueue_script( self::PRISM_HANDLE );
},
100
);
}
return $posts;
}
/**
* Enqueue the styles and scripts for the Prism syntax highlighter.
*
* @return void
*/
public static function register_prism_assets() {
$plugin = code_snippets();
wp_register_style(
self::PRISM_HANDLE,
plugins_url( 'dist/prism.css', $plugin->file ),
array(),
$plugin->version
);
wp_register_script(
self::PRISM_HANDLE,
plugins_url( 'dist/prism.js', $plugin->file ),
array(),
$plugin->version,
true
);
}
/**
* Enqueue all available Prism themes.
*
* @return void
*/
public static function enqueue_all_prism_themes() {
self::register_prism_assets();
wp_enqueue_style( self::PRISM_HANDLE );
wp_enqueue_script( self::PRISM_HANDLE );
}
/**
* Print a message to the user if the snippet ID attribute is invalid.
*
* @param integer $id Snippet ID.
*
* @return string Warning message.
*/
protected function invalid_id_warning( int $id ): string {
// translators: %d: snippet ID.
$text = esc_html__( 'Could not load snippet with an invalid ID: %d.', 'code-snippets' );
return current_user_can( 'edit_posts' ) ? sprintf( $text, $id ) : '';
}
/**
* Allow boolean attributes to be provided without a value, similar to how React works.
*
* @param array<string|number, mixed> $atts Unfiltered shortcode attributes.
* @param array<string> $boolean_flags List of attribute names with boolean values.
*
* @return array<string|number, mixed> Shortcode attributes with flags converted to attributes.
*/
protected function convert_boolean_attribute_flags( array $atts, array $boolean_flags ): array {
foreach ( $atts as $key => $value ) {
if ( in_array( $value, $boolean_flags, true ) && ! isset( $atts[ $value ] ) ) {
$atts[ $value ] = true;
unset( $atts[ $key ] );
}
}
return $atts;
}
/**
* Evaluate the code from a content shortcode.
*
* @param Snippet $snippet Snippet.
* @param array<string, mixed> $atts Shortcode attributes.
*
* @return string Evaluated shortcode content.
*/
protected function evaluate_shortcode_content( Snippet $snippet, array $atts ): string {
if ( empty( $atts['php'] ) ) {
return $snippet->code;
}
/**
* Avoiding extract is typically recommended, however in this situation we want to make it easy for snippet
* authors to use custom attributes.
*
* @phpcs:disable WordPress.PHP.DontExtract.extract_extract
*/
extract( $atts );
ob_start();
eval( "?>\n\n" . $snippet->code . "\n\n<?php" );
return ob_get_clean();
}
/**
* Render the value of a content shortcode
*
* @param array<string, mixed> $atts Shortcode attributes.
*
* @return string Shortcode content.
*/
public function render_content_shortcode( array $atts ): string {
$atts = $this->convert_boolean_attribute_flags( $atts, [ 'network', 'php', 'format', 'shortcodes', 'debug' ] );
$original_atts = $atts;
$atts = shortcode_atts(
[
'id' => 0,
'snippet_id' => 0,
'network' => false,
'php' => false,
'format' => false,
'shortcodes' => false,
'debug' => false,
],
$atts,
self::CONTENT_SHORTCODE
);
$id = 0 !== intval( $atts['snippet_id'] ) ? intval( $atts['snippet_id'] ) : intval( $atts['id'] );
if ( ! $id ) {
return $this->invalid_id_warning( $id );
}
$snippet = get_snippet( $id, (bool) $atts['network'] );
// Render the source code if this is not a shortcode snippet.
if ( 'content' !== $snippet->scope ) {
return $snippet->id ? $this->render_snippet_source( $snippet ) : $this->invalid_id_warning( $snippet->id );
}
// If the snippet is inactive, either display a message or render nothing.
if ( ! $snippet->active ) {
if ( ! $atts['debug'] ) {
return '';
}
/* translators: 1: snippet name, 2: snippet edit link */
$text = __( '<strong>%1$s</strong> is currently inactive. You can <a href="%2$s">edit this snippet</a> to activate it and make it visible. This message will not appear in the published post.', 'code-snippets' );
$edit_url = add_query_arg( 'id', $snippet->id, code_snippets()->get_menu_url( 'edit' ) );
return wp_kses(
sprintf( $text, $snippet->name, $edit_url ),
[
'strong' => [],
'a' => [
'href' => [],
],
]
);
}
$content = $this->evaluate_shortcode_content( $snippet, $original_atts );
if ( $atts['format'] ) {
$functions = [ 'wptexturize', 'convert_smilies', 'convert_chars', 'wpautop', 'capital_P_dangit' ];
foreach ( $functions as $function ) {
$content = call_user_func( $function, $content );
}
}
if ( $atts['shortcodes'] ) {
// Remove this shortcode from the list to prevent recursion.
remove_shortcode( self::CONTENT_SHORTCODE );
// Recursion depth is limited to prevent infinite loops.
static $depth = 0;
$max_depth = self::MAX_SHORTCODE_DEPTH;
// Find the shortcode in the content and replace it with the evaluated content.
$content = preg_replace_callback(
'/\[' . self::CONTENT_SHORTCODE . '([^\]]*)\]/',
function ($matches) use (&$depth, $max_depth) {
if ($depth >= $max_depth) {
return '<!-- Max shortcode depth reached -->';
}
$depth++;
$atts = shortcode_parse_atts($matches[1]);
$result = $this->render_content_shortcode($atts);
$depth--;
return $result;
},
$content
);
// Add this shortcode back to the list.
add_shortcode( self::CONTENT_SHORTCODE, [ $this, 'render_content_shortcode' ] );
}
return apply_filters( 'code_snippets/content_shortcode', $content, $snippet, $atts, $original_atts );
}
/**
* Converts a value and key into an HTML attribute pair.
*
* @param string $value Attribute value.
* @param string $key Attribute name.
*
* @return void
*/
private static function create_attribute_pair( string &$value, string $key ) {
$value = sprintf( '%s="%s"', sanitize_key( $key ), esc_attr( $value ) );
}
/**
* Render the source code of a given snippet
*
* @param Snippet $snippet Snippet object.
* @param array<string, mixed> $atts Shortcode attributes.
*
* @return string Shortcode content.
*/
private function render_snippet_source( Snippet $snippet, array $atts = [] ): string {
$atts = array_merge(
array(
'line_numbers' => false,
'highlight_lines' => '',
),
$atts
);
$language = 'css' === $snippet->type ? 'css' : ( 'js' === $snippet->type ? 'js' : 'php' );
$pre_attributes = array(
'id' => "code-snippet-source-$snippet->id",
'class' => 'code-snippet-source',
);
$code_attributes = array(
'class' => "language-$language",
);
if ( $atts['line_numbers'] ) {
$code_attributes['class'] .= ' line-numbers';
$pre_attributes['class'] .= ' linkable-line-numbers';
}
if ( $atts['highlight_lines'] ) {
$pre_attributes['data-line'] = $atts['highlight_lines'];
}
$pre_attributes = apply_filters( 'code_snippets/prism_pre_attributes', $pre_attributes, $snippet, $atts );
$code_attributes = apply_filters( 'code_snippets/prism_code_attributes', $code_attributes, $snippet, $atts );
array_walk( $code_attributes, array( $this, 'create_attribute_pair' ) );
array_walk( $pre_attributes, array( $this, 'create_attribute_pair' ) );
$code = 'php' === $snippet->type ? "<?php\n\n$snippet->code" : $snippet->code;
$output = sprintf(
'<pre %s><code %s>%s</code></pre>',
implode( ' ', $pre_attributes ),
implode( ' ', $code_attributes ),
esc_html( $code )
);
return apply_filters( 'code_snippets/render_source_shortcode', $output, $snippet, $atts );
}
/**
* Render the value of a source shortcode
*
* @param array<string, mixed> $atts Shortcode attributes.
*
* @return string Shortcode content.
*/
public function render_source_shortcode( array $atts ): string {
$atts = $this->convert_boolean_attribute_flags( $atts, [ 'network', 'line_numbers' ] );
$atts = shortcode_atts(
array(
'id' => 0,
'snippet_id' => 0,
'network' => false,
'line_numbers' => false,
'highlight_lines' => '',
),
$atts,
self::SOURCE_SHORTCODE
);
$id = 0 !== intval( $atts['snippet_id'] ) ? intval( $atts['snippet_id'] ) : intval( $atts['id'] );
if ( ! $id ) {
return $this->invalid_id_warning( $id );
}
$snippet = get_snippet( $id, (bool) $atts['network'] );
return $this->render_snippet_source( $snippet, $atts );
}
}