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

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

327 lines 8.3 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( 'dist/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 // Concatenate all provided content together, so we can check it all in one go.
89 $content = implode( ' ', wp_list_pluck( $posts, 'post_content' ) );
90
91 // Bail now if the provided post don't contain the source shortcode or block editor block.
92 if ( false === stripos( $content, '[' . self::SOURCE_SHORTCODE ) &&
93 false === strpos( $content, '<!-- wp:code-snippets/source ' ) ) {
94 return $posts;
95 }
96
97 // Load Prism assets on the appropriate hook.
98 $this->register_prism_assets();
99
100 add_action(
101 'wp_enqueue_scripts',
102 function () {
103 wp_enqueue_style( self::PRISM_HANDLE );
104 wp_enqueue_script( self::PRISM_HANDLE );
105 },
106 100
107 );
108
109 return $posts;
110 }
111
112 /**
113 * Enqueue the styles and scripts for the Prism syntax highlighter.
114 */
115 public static function register_prism_assets() {
116 $plugin = code_snippets();
117
118 wp_register_style(
119 self::PRISM_HANDLE,
120 plugins_url( 'dist/prism.css', $plugin->file ),
121 array(),
122 $plugin->version
123 );
124
125 wp_register_script(
126 self::PRISM_HANDLE,
127 plugins_url( 'dist/prism.js', $plugin->file ),
128 array(),
129 $plugin->version,
130 true
131 );
132 }
133
134 /**
135 * Print a message to the user if the snippet ID attribute is invalid.
136 *
137 * @param int $id Snippet ID.
138 *
139 * @return string Warning message.
140 */
141 private function invalid_id_warning( $id ) {
142 /* translators: %d: snippet ID */
143 $text = esc_html__( 'Could not load snippet with an invalid ID: %d.', 'code-snippets' );
144 return current_user_can( 'edit_posts' ) ? sprintf( $text, intval( $id ) ) : '';
145 }
146
147 /**
148 * Render the value of a content shortcode
149 *
150 * @param array $atts Shortcode attributes.
151 *
152 * @return string Shortcode content.
153 */
154 public function render_content_shortcode( $atts ) {
155 $atts = shortcode_atts(
156 array(
157 'id' => 0,
158 'snippet_id' => 0,
159 'network' => false,
160 'php' => false,
161 'format' => false,
162 'shortcodes' => false,
163 'debug' => false,
164 ),
165 $atts,
166 self::CONTENT_SHORTCODE
167 );
168
169 $id = intval( $atts['snippet_id'] ) ?: intval( $atts['id'] );
170 if ( ! $id ) {
171 return $this->invalid_id_warning( $id );
172 }
173
174 $snippet = get_snippet( $id, (bool) $atts['network'] );
175
176 // Render the source code if this is not a shortcode snippet.
177 if ( 'content' !== $snippet->scope ) {
178 return $snippet->id ? $this->render_snippet_source( $snippet ) : '';
179 }
180
181 // If the snippet is inactive, either display a message or render nothing.
182 if ( ! $snippet->active ) {
183 if ( ! $atts['debug'] ) {
184 return '';
185 }
186
187 /* translators: 1: snippet name, 2: snippet edit link */
188 $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' );
189
190 $edit_url = add_query_arg( 'id', $snippet->id, code_snippets()->get_menu_url( 'edit' ) );
191 return wp_kses(
192 sprintf( $text, $snippet->name, $edit_url ),
193 [
194 'strong' => [],
195 'a' => [
196 'href' => [],
197 ],
198 ]
199 );
200 }
201
202 $content = $snippet->code;
203
204 if ( $atts['php'] ) {
205 ob_start();
206 eval( "?>\n\n" . $snippet->code . "\n\n<?php" );
207 $content = ob_get_clean();
208 }
209
210 if ( $atts['format'] ) {
211 $functions = [ 'wptexturize', 'convert_smilies', 'convert_chars', 'wpautop', 'capital_P_dangit' ];
212 foreach ( $functions as $function ) {
213 $content = call_user_func( $function, $content );
214 }
215 }
216
217 if ( $atts['shortcodes'] ) {
218 // Remove this shortcode from the list to prevent recursion.
219 remove_shortcode( self::CONTENT_SHORTCODE );
220
221 // Evaluate shortcodes.
222 $content = do_shortcode( $atts['format'] ? shortcode_unautop( $content ) : $content );
223
224 // Add this shortcode back to the list.
225 add_shortcode( self::CONTENT_SHORTCODE, [ $this, 'render_content_shortcode' ] );
226 }
227
228 return $content;
229 }
230
231 /**
232 * Converts a value and key into an HTML attribute pair.
233 *
234 * @param string $value Attribute value.
235 * @param string $key Attribute name.
236 *
237 * @return void
238 */
239 private static function create_attribute_pair( &$value, $key ) {
240 $value = sprintf( '%s="%s"', sanitize_key( $key ), esc_attr( $value ) );
241 }
242
243 /**
244 * Render the source code of a given snippet
245 *
246 * @param Snippet $snippet Snippet object.
247 * @param array $atts Shortcode attributes.
248 *
249 * @return string Shortcode content.
250 */
251 private function render_snippet_source( Snippet $snippet, $atts = [] ) {
252 $atts = array_merge(
253 array(
254 'line_numbers' => false,
255 'highlight_lines' => '',
256 ),
257 $atts
258 );
259
260 $language = 'css' === $snippet->type ? 'css' : ( 'js' === $snippet->type ? 'js' : 'php' );
261
262 $pre_attributes = array(
263 'id' => "code-snippet-source-$snippet->id",
264 'class' => 'code-snippet-source',
265 );
266
267 $code_attributes = array(
268 'class' => "language-$language",
269 );
270
271 if ( $atts['line_numbers'] ) {
272 $code_attributes['class'] .= ' line-numbers';
273 $pre_attributes['class'] .= ' linkable-line-numbers';
274 }
275
276 if ( $atts['highlight_lines'] ) {
277 $pre_attributes['data-line'] = $atts['highlight_lines'];
278 }
279
280 $pre_attributes = apply_filters( 'code_snippets/prism_pre_attributes', $pre_attributes, $snippet, $atts );
281 $code_attributes = apply_filters( 'code_snippets/prism_code_attributes', $code_attributes, $snippet, $atts );
282
283 array_walk( $code_attributes, array( $this, 'create_attribute_pair' ) );
284 array_walk( $pre_attributes, array( $this, 'create_attribute_pair' ) );
285
286 $code = 'php' === $snippet->type ? "<?php\n\n$snippet->code" : $snippet->code;
287
288 return sprintf(
289 '<pre %s><code %s>%s</code></pre>',
290 implode( ' ', $pre_attributes ),
291 implode( ' ', $code_attributes ),
292 esc_html( $code )
293 );
294 }
295
296 /**
297 * Render the value of a source shortcode
298 *
299 * @param array $atts Shortcode attributes.
300 *
301 * @return string Shortcode content.
302 */
303 public function render_source_shortcode( $atts ) {
304
305 $atts = shortcode_atts(
306 array(
307 'id' => 0,
308 'snippet_id' => 0,
309 'network' => false,
310 'line_numbers' => false,
311 'highlight_lines' => '',
312 ),
313 $atts,
314 self::SOURCE_SHORTCODE
315 );
316
317 $id = intval( $atts['snippet_id'] ) ?: intval( $atts['id'] );
318 if ( ! $id ) {
319 return $this->invalid_id_warning( $id );
320 }
321
322 $snippet = get_snippet( $id, (bool) $atts['network'] );
323
324 return $this->render_snippet_source( $snippet, $atts );
325 }
326 }
327