PluginProbe
Code Snippets / 3.0.0
Code Snippets v3.0.0
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-frontend.php

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

284 lines 6.9 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 /**
6 * This class manages the shortcodes included with the plugin
7 *
8 * @package Code_Snippets
9 */
10 class Frontend {
11
12 /**
13 * Name of the shortcode tag for rendering the code source
14 */
15 const SOURCE_SHORTCODE = 'code_snippet_source';
16
17 /**
18 * Name of the shortcode tag for rendering content snippets
19 */
20 const CONTENT_SHORTCODE = 'code_snippet';
21
22 /**
23 * Handle to use for front-end scripts and styles.
24 */
25 const PRISM_HANDLE = 'code-snippets-prism';
26
27 /**
28 * Class constructor
29 */
30 public function __construct() {
31 add_action( 'the_posts', [ $this, 'enqueue_highlighting' ] );
32 add_action( 'init', [ $this, 'setup_mce_plugin' ] );
33
34 add_shortcode( self::CONTENT_SHORTCODE, [ $this, 'render_content_shortcode' ] );
35 add_shortcode( self::SOURCE_SHORTCODE, [ $this, 'render_source_shortcode' ] );
36 }
37
38 /**
39 * Perform the necessary actions to add a button to the TinyMCE editor
40 */
41 public function setup_mce_plugin() {
42 if ( ! code_snippets()->current_user_can() ) {
43 return;
44 }
45
46 /* Register the TinyMCE plugin */
47 add_filter(
48 'mce_external_plugins',
49 function ( $plugins ) {
50 $plugins['code_snippets'] = plugins_url( 'js/min/mce.js', PLUGIN_FILE );
51 return $plugins;
52 }
53 );
54
55 /* Add the button to the editor toolbar */
56 add_filter(
57 'mce_buttons',
58 function ( $buttons ) {
59 $buttons[] = 'code_snippets';
60 return $buttons;
61 }
62 );
63
64 /* Add the translation strings to the TinyMCE editor */
65 add_filter(
66 'mce_external_languages',
67 function ( $languages ) {
68 $languages['code_snippets'] = __DIR__ . '/mce-strings.php';
69 return $languages;
70 }
71 );
72 }
73
74 /**
75 * Enqueue the syntax highlighting assets if they are required for the current posts
76 *
77 * @param array $posts List of currently visible posts.
78 *
79 * @return array Unchanged list of posts.
80 */
81 public function enqueue_highlighting( $posts ) {
82
83 // exit early if there are no posts to check or if the highlighter has been disabled
84 if ( empty( $posts ) || Settings\get_setting( 'general', 'disable_prism' ) ) {
85 return $posts;
86 }
87
88 // loop through all the posts, checking for instances where the source tag is used
89 foreach ( $posts as $post ) {
90
91 if ( false === stripos( $post->post_content, '[' . self::SOURCE_SHORTCODE ) &&
92 ! ( function_exists( 'has_block' ) && has_block( 'code-snippets/source', $post ) ) ) {
93 continue;
94 }
95
96 // register the syntax highlighter assets and exit from the loop once a match is discovered
97 $this->register_prism_assets();
98 wp_enqueue_style( self::PRISM_HANDLE );
99 wp_enqueue_script( self::PRISM_HANDLE );
100 break;
101 }
102
103 return $posts;
104 }
105
106 /**
107 * Enqueue the styles and scripts for the Prism syntax highlighter.
108 */
109 public static function register_prism_assets() {
110 $plugin = code_snippets();
111
112 wp_register_style(
113 self::PRISM_HANDLE,
114 plugins_url( 'css/min/prism.css', $plugin->file ),
115 array(),
116 $plugin->version
117 );
118
119 wp_enqueue_script(
120 self::PRISM_HANDLE,
121 plugins_url( 'js/min/prism.js', $plugin->file ),
122 array(),
123 $plugin->version,
124 true
125 );
126 }
127
128 /**
129 * Print a message to the user if the snippet ID attribute is invalid.
130 *
131 * @param int $id Snippet ID.
132 *
133 * @return string Warning message.
134 */
135 private function invalid_id_warning( $id ) {
136 /* translators: %d: snippet ID */
137 $text = esc_html__( 'Could not load snippet with an invalid ID: %d.', 'code-snippets' );
138 return current_user_can( 'edit_posts' ) ? sprintf( $text, intval( $id ) ) : '';
139 }
140
141 /**
142 * Render the value of a content shortcode
143 *
144 * @param array $atts Shortcode attributes.
145 *
146 * @return string Shortcode content.
147 */
148 public function render_content_shortcode( $atts ) {
149 $atts = shortcode_atts(
150 array(
151 'id' => 0,
152 'snippet_id' => 0,
153 'network' => false,
154 'php' => false,
155 'format' => false,
156 'shortcodes' => false,
157 'debug' => false,
158 ),
159 $atts,
160 self::CONTENT_SHORTCODE
161 );
162
163 $id = intval( $atts['snippet_id'] ) ?: intval( $atts['id'] );
164 if ( ! $id ) {
165 return $this->invalid_id_warning( $id );
166 }
167
168 $snippet = get_snippet( $id, (bool) $atts['network'] );
169
170 // render the source code if this is not a shortcode snippet
171 if ( 'content' !== $snippet->scope ) {
172 return $snippet->id ? $this->render_snippet_source( $snippet ) : '';
173 }
174
175 // if the snippet is inactive, either display a message or render nothing.
176 if ( ! $snippet->active ) {
177 if ( ! $atts['debug'] ) {
178 return '';
179 }
180
181 /* translators: 1: snippet name, 2: snippet edit link */
182 $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' );
183
184 $edit_url = add_query_arg( 'id', $snippet->id, code_snippets()->get_menu_url( 'edit' ) );
185 return wp_kses(
186 sprintf( $text, $snippet->name, $edit_url ),
187 [
188 'strong' => [],
189 'a' => [
190 'href' => [],
191 ],
192 ]
193 );
194 }
195
196 $content = $snippet->code;
197
198 if ( $atts['php'] ) {
199 ob_start();
200 eval( "?>\n\n" . $snippet->code . "\n\n<?php" );
201 $content = ob_get_clean();
202 }
203
204 if ( $atts['format'] ) {
205 $functions = [ 'wptexturize', 'convert_smilies', 'convert_chars', 'wpautop', 'capital_P_dangit' ];
206 foreach ( $functions as $function ) {
207 $content = call_user_func( $function, $content );
208 }
209 }
210
211 if ( $atts['shortcodes'] ) {
212 // remove this shortcode from the list to prevent recursion
213 remove_shortcode( self::CONTENT_SHORTCODE );
214
215 // evaluate shortcodes
216 $content = do_shortcode( $atts['format'] ? shortcode_unautop( $content ) : $content );
217
218 // add this shortcode back to the list
219 add_shortcode( self::CONTENT_SHORTCODE, [ $this, 'render_content_shortcode' ] );
220 }
221
222 return $content;
223 }
224
225 /**
226 * Render the source code of a given snippet
227 *
228 * @param Snippet $snippet Snippet object.
229 * @param array $atts Shortcode attributes.
230 *
231 * @return string Shortcode content.
232 */
233 private function render_snippet_source( Snippet $snippet, $atts = [] ) {
234 $atts = array_merge( [ 'line_numbers' => false ], $atts );
235
236 if ( ! trim( $snippet->code ) ) {
237 return '';
238 }
239
240 $class = 'language-' . $snippet->type;
241
242 if ( $atts['line_numbers'] ) {
243 $class .= ' line-numbers';
244 }
245
246 return sprintf(
247 '<pre><code class="%s">%s</code></pre>',
248 $class,
249 esc_html( $snippet->code )
250 );
251 }
252
253 /**
254 * Render the value of a source shortcode
255 *
256 * @param array $atts Shortcode attributes.
257 *
258 * @return string Shortcode content.
259 */
260 public function render_source_shortcode( $atts ) {
261
262 $atts = shortcode_atts(
263 array(
264 'id' => 0,
265 'snippet_id' => 0,
266 'network' => false,
267 'line_numbers' => false,
268 ),
269 $atts,
270 self::SOURCE_SHORTCODE
271 );
272
273 $id = intval( $atts['snippet_id'] ) ?: intval( $atts['id'] );
274 if ( ! $id ) {
275 return $this->invalid_id_warning( $id );
276 }
277
278 $snippet = get_snippet( $id, (bool) $atts['network'] );
279
280 return $this->render_snippet_source( $snippet, $atts );
281 }
282 }
283
284