PluginProbe
Code Snippets / 2.14.6
Code Snippets v2.14.6
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 / class-shortcode.php

class-shortcode.php in Code Snippets 2.14.6, at php/class-shortcode.php

77 lines 1.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Code_Snippets_Shortcode {
4
5 public function __construct() {
6 add_shortcode( 'code_snippet', array( $this, 'render_shortcode' ) );
7 add_action( 'the_posts', array( $this, 'enqueue_assets' ) );
8 }
9
10 public function enqueue_assets( $posts ) {
11
12 if ( empty( $posts ) || code_snippets_get_setting( 'general', 'disable_prism' ) ) {
13 return $posts;
14 }
15
16 $found = false;
17
18 foreach ( $posts as $post ) {
19
20 if ( false !== stripos( $post->post_content, '[code_snippet' ) ) {
21 $found = true;
22 break;
23 }
24 }
25
26 if ( ! $found ) {
27 return $posts;
28 }
29
30 $plugin = code_snippets();
31
32 wp_enqueue_style(
33 'code-snippets-front-end',
34 plugins_url( 'css/min/front-end.css', $plugin->file ),
35 array(),
36 $plugin->version
37 );
38
39 wp_enqueue_script(
40 'code-snippets-front-end',
41 plugins_url( 'js/min/front-end.js', $plugin->file ),
42 array(),
43 $plugin->version,
44 true
45 );
46
47 return $posts;
48 }
49
50 public function render_shortcode( $atts ) {
51
52 $atts = shortcode_atts(
53 array(
54 'id' => 0,
55 'network' => false,
56 ),
57 $atts,
58 'code_snippet'
59 );
60
61 $id = intval( $atts['id'] );
62 if ( ! $id ) {
63 return '';
64 }
65
66 $network = $atts['network'] ? true : false;
67 $snippet = get_snippet( $id, $network );
68
69 if ( ! trim( $snippet->code ) ) {
70 return '';
71 }
72
73 return '<pre><code class="language-php">' . esc_html( $snippet->code ) . '</code></pre>';
74 }
75 }
76
77