PluginProbe
Code Snippets / 3.8.1
Code Snippets v3.8.1
3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 3.0.1 All 64 releases
code-snippets / php / front-end / class-front-end.php

class-front-end.php in Code Snippets 3.8.1, at php/front-end/class-front-end.php

502 lines 14.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Code_Snippets;
4
5 use WP_Post;
6 use WP_REST_Response;
7 use WP_REST_Server;
8
9 /**
10 * This class manages the shortcodes included with the plugin,
11 *
12 * @package Code_Snippets
13 */
14 class Front_End {
15
16 /**
17 * Name of the shortcode tag for rendering the code source
18 */
19 public const SOURCE_SHORTCODE = 'code_snippet_source';
20
21 /**
22 * Name of the shortcode tag for rendering content snippets
23 */
24 public const CONTENT_SHORTCODE = 'code_snippet';
25
26 /**
27 * Handle to use for front-end scripts and styles.
28 */
29 public const PRISM_HANDLE = 'code-snippets-prism';
30
31 /**
32 * Class constructor
33 */
34 public function __construct() {
35 add_action( 'the_posts', [ $this, 'enqueue_highlighting' ] );
36 add_action( 'init', [ $this, 'setup_mce_plugin' ] );
37
38 add_shortcode( self::CONTENT_SHORTCODE, [ $this, 'render_content_shortcode' ] );
39 add_shortcode( self::SOURCE_SHORTCODE, [ $this, 'render_source_shortcode' ] );
40
41 add_filter( 'code_snippets/render_content_shortcode', 'trim' );
42 }
43
44 /**
45 * Register REST API routes for use in front-end plugins.
46 *
47 * @return void
48 */
49 public function register_rest_routes() {
50 register_rest_route(
51 'v1/snippets',
52 '/snippets-info',
53 array(
54 'methods' => WP_REST_Server::READABLE,
55 'callback' => [ $this, 'get_snippets_info' ],
56 'permission_callback' => function () {
57 return current_user_can( 'edit_posts' );
58 },
59 )
60 );
61 }
62
63 /**
64 * Fetch snippets data in response to a request.
65 *
66 * @return WP_REST_Response
67 */
68 public function get_snippets_info(): WP_REST_Response {
69 $snippets = get_snippets();
70 $data = [];
71
72 foreach ( $snippets as $snippet ) {
73 $data[] = [
74 'id' => $snippet->id,
75 'name' => $snippet->name,
76 'type' => $snippet->type,
77 'active' => $snippet->active,
78 ];
79 }
80
81 return new WP_REST_Response( $data, 200 );
82 }
83
84 /**
85 * Perform the necessary actions to add a button to the TinyMCE editor
86 */
87 public function setup_mce_plugin() {
88 if ( ! code_snippets()->current_user_can() ) {
89 return;
90 }
91
92 /* Register the TinyMCE plugin */
93 add_filter(
94 'mce_external_plugins',
95 function ( $plugins ) {
96 $plugins['code_snippets'] = plugins_url( 'dist/mce.js', PLUGIN_FILE );
97 return $plugins;
98 }
99 );
100
101 /* Add the button to the editor toolbar */
102 add_filter(
103 'mce_buttons',
104 function ( $buttons ) {
105 $buttons[] = 'code_snippets';
106 return $buttons;
107 }
108 );
109
110 /* Add the translation strings to the TinyMCE editor */
111 add_filter(
112 'mce_external_languages',
113 function ( $languages ) {
114 $languages['code_snippets'] = __DIR__ . '/mce-strings.php';
115 return $languages;
116 }
117 );
118 }
119
120 /**
121 * Enqueue the syntax highlighting assets if they are required for the current posts
122 *
123 * @param array<WP_Post|int>|null|false $posts List of currently visible posts.
124 *
125 * @return array<WP_Post|int>|null|false Unchanged list of posts.
126 */
127 public function enqueue_highlighting( $posts ) {
128
129 // Exit early if there are no posts to check or if the highlighter has been disabled.
130 if ( empty( $posts ) || Settings\get_setting( 'general', 'disable_prism' ) ) {
131 return $posts;
132 }
133
134 // Loop through the posts, checking for an existing shortcode, short-circuiting if possible.
135 $found_shortcode_content = null;
136
137 foreach ( $posts as $post ) {
138 if ( false !== stripos( $post->post_content, '[' . self::SOURCE_SHORTCODE ) ||
139 false !== strpos( $post->post_content, '<!-- wp:code-snippets/source ' ) ) {
140 $found_shortcode_content = $post->post_content;
141 break;
142 }
143 }
144
145 // Load assets on the appropriate hook if a matching shortcode was found.
146 if ( null !== $found_shortcode_content ) {
147 $this->register_prism_assets();
148
149 add_action(
150 'wp_enqueue_scripts',
151 function () {
152 wp_enqueue_style( self::PRISM_HANDLE );
153 wp_enqueue_script( self::PRISM_HANDLE );
154 },
155 100
156 );
157 }
158
159 return $posts;
160 }
161
162 /**
163 * Enqueue the styles and scripts for the Prism syntax highlighter.
164 *
165 * @return void
166 */
167 public static function register_prism_assets() {
168 $plugin = code_snippets();
169
170 wp_register_script(
171 self::PRISM_HANDLE,
172 plugins_url( 'dist/prism.js', $plugin->file ),
173 array(),
174 $plugin->version,
175 true
176 );
177
178 wp_register_style(
179 self::PRISM_HANDLE,
180 plugins_url( 'dist/prism.css', $plugin->file ),
181 array(),
182 $plugin->version
183 );
184 }
185
186 /**
187 * Enqueue all available Prism themes.
188 *
189 * @return void
190 */
191 public static function enqueue_all_prism_themes() {
192 self::register_prism_assets();
193
194 wp_enqueue_style( self::PRISM_HANDLE );
195 wp_enqueue_script( self::PRISM_HANDLE );
196 }
197
198 /**
199 * Print a message to the user if the snippet ID attribute is invalid.
200 *
201 * @param integer $id Snippet ID.
202 *
203 * @return string Warning message.
204 */
205 protected function invalid_id_warning( int $id ): string {
206 // translators: %d: snippet ID.
207 $text = esc_html__( 'Could not load snippet with an invalid ID: %d.', 'code-snippets' );
208 return current_user_can( 'edit_posts' ) ? sprintf( $text, $id ) : '';
209 }
210
211 /**
212 * Allow boolean attributes to be provided without a value, similar to how React works.
213 *
214 * @param array<string|number, mixed> $atts Unfiltered shortcode attributes.
215 * @param array<string> $boolean_flags List of attribute names with boolean values.
216 *
217 * @return array<string|number, mixed> Shortcode attributes with flags converted to attributes.
218 */
219 protected function convert_boolean_attribute_flags( array $atts, array $boolean_flags ): array {
220 foreach ( $atts as $key => $value ) {
221 if ( in_array( $value, $boolean_flags, true ) && ! isset( $atts[ $value ] ) ) {
222 $atts[ $value ] = true;
223 unset( $atts[ $key ] );
224 }
225 }
226
227 return $atts;
228 }
229
230 /**
231 * Build the file path for a snippet's flat file.
232 *
233 * @param string $table_name Table name for the snippet.
234 * @param Snippet $snippet Snippet object.
235 *
236 * @return string Full file path for the snippet.
237 */
238 private function build_snippet_flat_file_path( string $table_name, Snippet $snippet ): string {
239 $handler = code_snippets()->snippet_handler_registry->get_handler( $snippet->get_type() );
240
241 return Snippet_Files::get_base_dir( $table_name, $handler->get_dir_name() ) . '/' . $snippet->id . '.' . $handler->get_file_extension();
242 }
243
244 /**
245 * Evaluate the code from a content shortcode.
246 *
247 * @param Snippet $snippet Snippet.
248 * @param array<string, mixed> $atts Shortcode attributes.
249 *
250 * @return string Evaluated shortcode content.
251 */
252 protected function evaluate_shortcode_content( Snippet $snippet, array $atts ): string {
253 if ( empty( $atts['php'] ) ) {
254 return $snippet->code;
255 }
256
257 if ( ! Snippet_Files::is_active() ) {
258 return $this->evaluate_shortcode_from_db( $snippet, $atts );
259 }
260
261 $network = DB::validate_network_param( $snippet->network );
262 $table_name = Snippet_Files::get_hashed_table_name( code_snippets()->db->get_table_name( $network ) );
263 $filepath = $this->build_snippet_flat_file_path( $table_name, $snippet );
264
265 return file_exists( $filepath )
266 ? $this->evaluate_shortcode_from_flat_file( $filepath, $atts )
267 : $this->evaluate_shortcode_from_db( $snippet, $atts );
268 }
269
270 private function evaluate_shortcode_from_db( Snippet $snippet, array $atts ): string {
271 /**
272 * Avoiding extract is typically recommended, however in this situation we want to make it easy for snippet
273 * authors to use custom attributes.
274 *
275 * @phpcs:disable WordPress.PHP.DontExtract.extract_extract
276 */
277 extract( $atts );
278
279 ob_start();
280 eval( "?>\n\n" . $snippet->code );
281
282 return ob_get_clean();
283 }
284
285 private function evaluate_shortcode_from_flat_file( $filepath, array $atts ): string {
286 ob_start();
287
288 ( function( $atts ) use ( $filepath ) {
289 /**
290 * Avoiding extract is typically recommended, however in this situation we want to make it easy for snippet
291 * authors to use custom attributes.
292 *
293 * @phpcs:disable WordPress.PHP.DontExtract.extract_extract
294 */
295 extract( $atts );
296 require_once $filepath;
297 } )( $atts );
298
299 return ob_get_clean();
300 }
301
302 private function get_snippet( int $id, bool $network, string $snippet_type ): Snippet {
303 if ( ! Snippet_Files::is_active() ) {
304 return get_snippet( $id, $network );
305 }
306
307 $validated_network = DB::validate_network_param( $network );
308 $table_name = Snippet_Files::get_hashed_table_name( code_snippets()->db->get_table_name( $validated_network ) );
309 $handler = code_snippets()->snippet_handler_registry->get_handler( $snippet_type );
310 $config_filepath = Snippet_Files::get_base_dir( $table_name, $handler->get_dir_name() ) . '/index.php';
311
312 if ( file_exists( $config_filepath ) ) {
313 $config = require_once $config_filepath;
314 $snippet_data = $config[ $id ] ?? null;
315
316 if ( $snippet_data ) {
317 $snippet = new Snippet( $snippet_data );
318 return apply_filters( 'code_snippets/get_snippet', $snippet, $id, $network );
319 }
320 }
321
322 return get_snippet( $id, $network );
323 }
324
325 /**
326 * Render the value of a content shortcode
327 *
328 * @param array<string, mixed> $atts Shortcode attributes.
329 *
330 * @return string Shortcode content.
331 */
332 public function render_content_shortcode( array $atts ): string {
333 $atts = $this->convert_boolean_attribute_flags( $atts, [ 'network', 'php', 'format', 'shortcodes', 'debug' ] );
334 $original_atts = $atts;
335
336 $atts = shortcode_atts(
337 [
338 'id' => 0,
339 'snippet_id' => 0,
340 'network' => false,
341 'php' => false,
342 'format' => false,
343 'shortcodes' => false,
344 'debug' => false,
345 ],
346 $atts,
347 self::CONTENT_SHORTCODE
348 );
349
350 $id = 0 !== intval( $atts['snippet_id'] ) ? intval( $atts['snippet_id'] ) : intval( $atts['id'] );
351 if ( ! $id ) {
352 return $this->invalid_id_warning( $id );
353 }
354
355 $snippet = $this->get_snippet( $id, (bool) $atts['network'], 'html' );
356
357 // Render the source code if this is not a shortcode snippet.
358 if ( 'content' !== $snippet->scope ) {
359 return $snippet->id ? $this->render_snippet_source( $snippet ) : $this->invalid_id_warning( $snippet->id );
360 }
361
362 // If the snippet is inactive, either display a message or render nothing.
363 if ( ! $snippet->active ) {
364 if ( ! $atts['debug'] ) {
365 return '';
366 }
367
368 /* translators: 1: snippet name, 2: snippet edit link */
369 $text = __( '%1$s 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' );
370 $snippet_name = '<strong>' . $snippet->name . '</strong>';
371 $edit_url = add_query_arg( 'id', $snippet->id, code_snippets()->get_menu_url( 'edit' ) );
372
373 return wp_kses(
374 sprintf( $text, $snippet_name, $edit_url ),
375 [
376 'strong' => [],
377 'a' => [
378 'href' => [],
379 ],
380 ]
381 );
382 }
383
384 $content = $this->evaluate_shortcode_content( $snippet, $original_atts );
385
386 if ( $atts['format'] ) {
387 $functions = [ 'wptexturize', 'convert_smilies', 'convert_chars', 'wpautop', 'capital_P_dangit' ];
388 foreach ( $functions as $function ) {
389 $content = call_user_func( $function, $content );
390 }
391 }
392
393 if ( $atts['shortcodes'] ) {
394 // Temporarily remove this shortcode from the list to prevent recursion while executing do_shortcode.
395 remove_shortcode( self::CONTENT_SHORTCODE );
396 $content = do_shortcode( $atts['format'] ? shortcode_unautop( $content ) : $content );
397 add_shortcode( self::CONTENT_SHORTCODE, [ $this, 'render_content_shortcode' ] );
398 }
399
400 return apply_filters( 'code_snippets/content_shortcode', $content, $snippet, $atts, $original_atts );
401 }
402
403 /**
404 * Converts a value and key into an HTML attribute pair.
405 *
406 * @param string $value Attribute value.
407 * @param string $key Attribute name.
408 *
409 * @return void
410 */
411 private static function create_attribute_pair( string &$value, string $key ) {
412 $value = sprintf( '%s="%s"', sanitize_key( $key ), esc_attr( $value ) );
413 }
414
415 /**
416 * Render the source code of a given snippet
417 *
418 * @param Snippet $snippet Snippet object.
419 * @param array<string, mixed> $atts Shortcode attributes.
420 *
421 * @return string Shortcode content.
422 */
423 private function render_snippet_source( Snippet $snippet, array $atts = [] ): string {
424 $atts = array_merge(
425 array(
426 'line_numbers' => false,
427 'highlight_lines' => '',
428 ),
429 $atts
430 );
431
432 $language = 'css' === $snippet->type ? 'css' : ( 'js' === $snippet->type ? 'js' : 'php' );
433
434 $pre_attributes = array(
435 'id' => "code-snippet-source-$snippet->id",
436 'class' => 'code-snippet-source',
437 );
438
439 $code_attributes = array(
440 'class' => "language-$language",
441 );
442
443 if ( $atts['line_numbers'] ) {
444 $code_attributes['class'] .= ' line-numbers';
445 $pre_attributes['class'] .= ' linkable-line-numbers';
446 }
447
448 if ( $atts['highlight_lines'] ) {
449 $pre_attributes['data-line'] = $atts['highlight_lines'];
450 }
451
452 $pre_attributes = apply_filters( 'code_snippets/prism_pre_attributes', $pre_attributes, $snippet, $atts );
453 $code_attributes = apply_filters( 'code_snippets/prism_code_attributes', $code_attributes, $snippet, $atts );
454
455 array_walk( $code_attributes, array( $this, 'create_attribute_pair' ) );
456 array_walk( $pre_attributes, array( $this, 'create_attribute_pair' ) );
457
458 $code = 'php' === $snippet->type ? "<?php\n\n$snippet->code" : $snippet->code;
459
460 $output = sprintf(
461 '<pre %s><code %s>%s</code></pre>',
462 implode( ' ', $pre_attributes ),
463 implode( ' ', $code_attributes ),
464 esc_html( $code )
465 );
466
467 return apply_filters( 'code_snippets/render_source_shortcode', $output, $snippet, $atts );
468 }
469
470 /**
471 * Render the value of a source shortcode
472 *
473 * @param array<string, mixed> $atts Shortcode attributes.
474 *
475 * @return string Shortcode content.
476 */
477 public function render_source_shortcode( array $atts ): string {
478 $atts = $this->convert_boolean_attribute_flags( $atts, [ 'network', 'line_numbers' ] );
479
480 $atts = shortcode_atts(
481 array(
482 'id' => 0,
483 'snippet_id' => 0,
484 'network' => false,
485 'line_numbers' => false,
486 'highlight_lines' => '',
487 ),
488 $atts,
489 self::SOURCE_SHORTCODE
490 );
491
492 $id = 0 !== intval( $atts['snippet_id'] ) ? intval( $atts['snippet_id'] ) : intval( $atts['id'] );
493 if ( ! $id ) {
494 return $this->invalid_id_warning( $id );
495 }
496
497 $snippet = $this->get_snippet( $id, (bool) $atts['network'], 'html' );
498
499 return $this->render_snippet_source( $snippet, $atts );
500 }
501 }
502