# code-snippets/2.12.1/php/class-shortcode.php

Code Snippets, version 2.12.1. 72 lines.

- Page: https://pluginprobe.com/plugins/code-snippets/2.12.1/code/php/class-shortcode.php
- Raw: https://pluginprobe.com/plugins/code-snippets/2.12.1/raw/php/class-shortcode.php
- Modified: 2018-12-17T15:08:28+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/code-snippets/2.12.1/code/php/class-shortcode.php#L10-L20`.

```php
<?php

class Code_Snippets_Shortcode {

	function __construct() {
		add_shortcode( 'code_snippet', array( $this, 'render_shortcode' ) );
		add_action( 'the_posts', array( $this, 'enqueue_prism' ) );
	}

	function enqueue_prism( $posts ) {

		if ( empty( $posts ) || code_snippets_get_setting( 'general', 'disable_prism' ) ) {
			return $posts;
		}

		$found = false;

		foreach ( $posts as $post ) {

			if ( false !== stripos( $post->post_content, '[code_snippet' ) ) {
				$found = true;
				break;
			}
		}

		if ( ! $found ) {
			return $posts;
		}

		$plugin = code_snippets();

		wp_enqueue_style(
			'code-snippets-prism',
			plugins_url( 'js/vendor/prism.css', $plugin->file ),
			array(), $plugin->version
		);

		wp_enqueue_script(
			'code-snippets-prism',
			plugins_url( 'js/vendor/prism.js', $plugin->file ),
			array(), $plugin->version, true
		);

		return $posts;
	}

	function render_shortcode( $atts ) {

		$atts = shortcode_atts(
			array(
				'id'      => 0,
				'network' => false,
			),
			$atts, 'code_snippet'
		);

		if ( ! $id = intval( $atts['id'] ) ) {
			return '';
		}

		$network = $atts['network'] ? true : false;
		$snippet = get_snippet( $id, $network );

		if ( ! trim( $snippet->code ) ) {
			return '';
		}

		return '<pre><code class="language-php">' . esc_html( $snippet->code ) . '</code></pre>';
	}
}


```
