PluginProbe
Code Snippets / 2.12.1
Code Snippets v2.12.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 / admin-menus / class-edit-menu.php

class-edit-menu.php in Code Snippets 2.12.1, at php/admin-menus/class-edit-menu.php

559 lines 16.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * This class handles the add/edit menu
5 */
6 class Code_Snippets_Edit_Menu extends Code_Snippets_Admin_Menu {
7
8 /**
9 * Constructor
10 */
11 public function __construct() {
12 parent::__construct( 'edit',
13 __( 'Edit Snippet', 'code-snippets' ),
14 __( 'Edit Snippet', 'code-snippets' )
15 );
16 }
17
18 /**
19 * Register action and filter hooks
20 */
21 public function run() {
22 parent::run();
23 $this->remove_debug_bar_codemirror();
24 }
25
26 /**
27 * Register the admin menu
28 */
29 public function register() {
30
31 /* Add edit menu if we are currently editing a snippet */
32 if ( isset( $_REQUEST['page'] ) && code_snippets()->get_menu_slug( 'edit' ) === $_REQUEST['page'] ) {
33 parent::register();
34 }
35
36 /* Add New Snippet menu */
37 $this->add_menu(
38 code_snippets()->get_menu_slug( 'add' ),
39 _x( 'Add New', 'menu label', 'code-snippets' ),
40 __( 'Add New Snippet', 'code-snippets' )
41 );
42 }
43
44 /**
45 * Executed when the menu is loaded
46 */
47 public function load() {
48 parent::load();
49
50 /* Don't allow visiting the edit snippet page without a valid ID */
51 if ( code_snippets()->get_menu_slug( 'edit' ) === $_REQUEST['page'] ) {
52 if ( ! isset( $_REQUEST['id'] ) || 0 == $_REQUEST['id'] ) {
53 wp_redirect( code_snippets()->get_menu_url( 'add' ) );
54 exit;
55 }
56 }
57
58 /* Load the contextual help tabs */
59 $contextual_help = new Code_Snippets_Contextual_Help( 'edit' );
60 $contextual_help->load();
61
62 /* Enqueue the code editor and other scripts and styles */
63 add_action( 'admin_enqueue_scripts', 'code_snippets_enqueue_codemirror' );
64 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_tagit' ), 9 );
65
66 /* Register action hooks */
67 if ( code_snippets_get_setting( 'general', 'enable_description' ) ) {
68 add_action( 'code_snippets/admin/single', array( $this, 'render_description_editor' ), 9 );
69 }
70
71 if ( code_snippets_get_setting( 'general', 'enable_tags' ) ) {
72 add_action( 'code_snippets/admin/single', array( $this, 'render_tags_editor' ) );
73 }
74
75 add_action( 'code_snippets/admin/single', array( $this, 'render_priority_setting' ), 0 );
76
77 if ( code_snippets_get_setting( 'general', 'snippet_scope_enabled' ) ) {
78 add_action( 'code_snippets/admin/single', array( $this, 'render_scope_setting' ), 1 );
79 }
80
81 if ( is_network_admin() ) {
82 add_action( 'code_snippets/admin/single', array( $this, 'render_multisite_sharing_setting' ), 1 );
83 }
84
85 $this->process_actions();
86 }
87
88 /**
89 * Process data sent from the edit page
90 */
91 private function process_actions() {
92
93 /* Check for a valid nonce */
94 if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'save_snippet' ) ) {
95 return;
96 }
97
98 if ( isset( $_POST['save_snippet'] ) || isset( $_POST['save_snippet_execute'] ) ||
99 isset( $_POST['save_snippet_activate'] ) || isset( $_POST['save_snippet_deactivate'] ) ) {
100 $this->save_posted_snippet();
101 }
102
103 if ( isset( $_POST['snippet_id'] ) ) {
104
105 /* Delete the snippet if the button was clicked */
106 if ( isset( $_POST['delete_snippet'] ) ) {
107 delete_snippet( $_POST['snippet_id'] );
108 wp_redirect( add_query_arg( 'result', 'delete', code_snippets()->get_menu_url( 'manage' ) ) );
109 exit;
110 }
111
112 /* Export the snippet if the button was clicked */
113 if ( isset( $_POST['export_snippet'] ) ) {
114 export_snippets( array( $_POST['snippet_id'] ) );
115 }
116
117 /* Download the snippet if the button was clicked */
118 if ( isset( $_POST['download_snippet'] ) ) {
119 download_snippets( array( $_POST['snippet_id'] ) );
120 }
121 }
122 }
123
124 /**
125 * Remove the sharing status from a network snippet
126 *
127 * @param int $snippet_id
128 */
129 private function unshare_network_snippet( $snippet_id ) {
130 $shared_snippets = get_site_option( 'shared_network_snippets', array() );
131
132 if ( ! in_array( $snippet_id, $shared_snippets ) ) {
133 return;
134 }
135
136 /* Remove the snippet ID from the array */
137 $shared_snippets = array_diff( $shared_snippets, array( $snippet_id ) );
138 update_site_option( 'shared_network_snippets', array_values( $shared_snippets ) );
139
140 /* Deactivate on all sites */
141 global $wpdb;
142 if ( $sites = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ) ) {
143
144 foreach ( $sites as $site ) {
145 switch_to_blog( $site );
146 $active_shared_snippets = get_option( 'active_shared_network_snippets' );
147
148 if ( is_array( $active_shared_snippets ) ) {
149 $active_shared_snippets = array_diff( $active_shared_snippets, array( $snippet_id ) );
150 update_option( 'active_shared_network_snippets', $active_shared_snippets );
151 }
152 }
153
154 restore_current_blog();
155 }
156 }
157
158 private function code_error_callback( $out ) {
159 $error = error_get_last();
160
161 if ( is_null( $error ) ) {
162 return $out;
163 }
164
165 $m = '<h3>' . __( "Don't Panic", 'code-snippets' ) . '</h3>';
166 /* translators: %d: line where error was produced */
167 $m .= '<p>' . sprintf( __( 'The code snippet you are trying to save produced a fatal error on line %d:', 'code-snippets' ), $error['line'] ) . '</p>';
168 $m .= '<strong>' . $error['message'] . '</strong>';
169 $m .= '<p>' . __( 'The previous version of the snippet is unchanged, and the rest of this site should be functioning normally as before.', 'code-snippets' ) . '</p>';
170 $m .= '<p>' . __( 'Please use the back button in your browser to return to the previous page and try to fix the code error.', 'code-snippets' );
171 $m .= ' ' . __( 'If you prefer, you can close this page and discard the changes you just made. No changes will be made to this site.', 'code-snippets' ) . '</p>';
172
173 return $m;
174 }
175
176 /**
177 * Validate the snippet code before saving to database
178 *
179 * @param Code_Snippet $snippet
180 *
181 * @return bool true if code produces errors
182 */
183 private function validate_code( Code_Snippet $snippet ) {
184
185 if ( empty( $snippet->code ) ) {
186 return false;
187 }
188
189 ob_start( array( $this, 'code_error_callback' ) );
190
191 $result = eval( $snippet->code );
192
193 ob_end_clean();
194
195 do_action( 'code_snippets/after_execute_snippet', $snippet->id, $snippet->code, $result );
196
197 return false === $result;
198 }
199
200 /**
201 * Save the posted snippet data to the database and redirect
202 */
203 private function save_posted_snippet() {
204
205 /* Build snippet object from fields with 'snippet_' prefix */
206 $snippet = new Code_Snippet();
207
208 foreach ( $_POST as $field => $value ) {
209 if ( 'snippet_' === substr( $field, 0, 8 ) ) {
210
211 /* Remove the 'snippet_' prefix from field name and set it on the object */
212 $snippet->set_field( substr( $field, 8 ), stripslashes( $value ) );
213 }
214 }
215
216 if ( isset( $_POST['save_snippet_execute'] ) && 'single-use' !== $snippet->scope ) {
217 unset( $_POST['save_snippet_execute'] );
218 $_POST['save_snippet'] = 'yes';
219 }
220
221 /* Activate or deactivate the snippet before saving if we clicked the button */
222
223 if ( isset( $_POST['save_snippet_execute'] ) ) {
224 $snippet->active = 1;
225 } elseif ( isset( $_POST['snippet_sharing'] ) && 'on' === $_POST['snippet_sharing'] ) {
226 // Shared network snippets cannot be network activated
227 $snippet->active = 0;
228 unset( $_POST['save_snippet_activate'], $_POST['save_snippet_deactivate'] );
229 } elseif ( isset( $_POST['save_snippet_activate'] ) ) {
230 $snippet->active = 1;
231 } elseif ( isset( $_POST['save_snippet_deactivate'] ) ) {
232 $snippet->active = 0;
233 }
234
235 /* Deactivate snippet if code contains errors */
236 if ( $snippet->active && 'single-use' !== $snippet->scope ) {
237 if ( $code_error = $this->validate_code( $snippet ) ) {
238 $snippet->active = 0;
239 }
240 }
241
242 /* Save the snippet to the database */
243 $snippet_id = save_snippet( $snippet );
244
245 /* Update the shared network snippets if necessary */
246 if ( $snippet_id && is_network_admin() ) {
247
248 if ( isset( $_POST['snippet_sharing'] ) && 'on' === $_POST['snippet_sharing'] ) {
249 $shared_snippets = get_site_option( 'shared_network_snippets', array() );
250
251 /* Add the snippet ID to the array if it isn't already */
252 if ( ! in_array( $snippet_id, $shared_snippets ) ) {
253 $shared_snippets[] = $snippet_id;
254 update_site_option( 'shared_network_snippets', array_values( $shared_snippets ) );
255 }
256 } else {
257 $this->unshare_network_snippet( $snippet_id );
258 }
259 }
260
261 /* If the saved snippet ID is invalid, display an error message */
262 if ( ! $snippet_id || $snippet_id < 1 ) {
263 /* An error occurred */
264 wp_redirect( add_query_arg( 'result', 'save-error', code_snippets()->get_menu_url( 'add' ) ) );
265 exit;
266 }
267
268 /* Display message if a parse error occurred */
269 if ( isset( $code_error ) && $code_error ) {
270 wp_redirect( add_query_arg(
271 array( 'id' => $snippet_id, 'result' => 'code-error' ),
272 code_snippets()->get_menu_url( 'edit' )
273 ) );
274 exit;
275 }
276
277 /* Set the result depending on if the snippet was just added */
278 $result = isset( $_POST['snippet_id'] ) ? 'updated' : 'added';
279
280 /* Append a suffix if the snippet was activated or deactivated */
281 if ( isset( $_POST['save_snippet_activate'] ) ) {
282 $result .= '-and-activated';
283 } elseif ( isset( $_POST['save_snippet_deactivate'] ) ) {
284 $result .= '-and-deactivated';
285 } elseif ( isset( $_POST['save_snippet_execute'] ) ) {
286 $result .= '-and-executed';
287 }
288
289 /* Redirect to edit snippet page */
290 $redirect_uri = add_query_arg(
291 array( 'id' => $snippet_id, 'result' => $result ),
292 code_snippets()->get_menu_url( 'edit' )
293 );
294
295 if ( isset( $_POST['snippet_editor_cursor'] ) ) {
296 $redirect_uri = add_query_arg( 'cursor', $_POST['snippet_editor_cursor'], $redirect_uri );
297 }
298
299 wp_redirect( esc_url_raw( $redirect_uri ) );
300 exit;
301 }
302
303 /**
304 * Add a description editor to the single snippet page
305 *
306 * @param Code_Snippet $snippet The snippet being used for this page
307 */
308 function render_description_editor( Code_Snippet $snippet ) {
309 $settings = code_snippets_get_settings();
310 $settings = $settings['description_editor'];
311 $heading = __( 'Description', 'code-snippets' );
312
313 /* Hack to remove space between heading and editor tabs */
314 if ( ! $settings['media_buttons'] && 'false' !== get_user_option( 'rich_editing' ) ) {
315 $heading = "<div>$heading</div>";
316 }
317
318 echo '<h2><label for="snippet_description">', $heading, '</label></h2>';
319
320 remove_editor_styles(); // stop custom theme styling interfering with the editor
321
322 wp_editor(
323 $snippet->desc,
324 'description',
325 apply_filters( 'code_snippets/admin/description_editor_settings', array(
326 'textarea_name' => 'snippet_description',
327 'textarea_rows' => $settings['rows'],
328 'teeny' => ! $settings['use_full_mce'],
329 'media_buttons' => $settings['media_buttons'],
330 ) )
331 );
332 }
333
334 /**
335 * Render the interface for editing snippet tags
336 *
337 * @param Code_Snippet $snippet the snippet currently being edited
338 */
339 function render_tags_editor( Code_Snippet $snippet ) {
340
341 ?>
342 <h2 style="margin: 25px 0 10px;">
343 <label for="snippet_tags" style="cursor: auto;">
344 <?php esc_html_e( 'Tags', 'code-snippets' ); ?>
345 </label>
346 </h2>
347
348 <input type="text" id="snippet_tags" name="snippet_tags" style="width: 100%;"
349 placeholder="<?php esc_html_e( 'Enter a list of tags; separated by commas', 'code-snippets' ); ?>"
350 value="<?php echo esc_attr( $snippet->tags_list ); ?>" />
351
352 <script type="text/javascript">
353 jQuery('#snippet_tags').tagit({
354 availableTags: <?php echo wp_json_encode( get_all_snippet_tags() ); ?>,
355 allowSpaces: true,
356 removeConfirmation: true,
357 showAutocompleteOnFocus: true,
358 });
359 </script>
360 <?php
361 }
362
363 /**
364 * Render the snippet priority setting
365 *
366 * @param Code_Snippet $snippet the snippet currently being edited
367 */
368 public function render_priority_setting( Code_Snippet $snippet ) {
369 ?>
370 <p class="snippet-priority"
371 title="<?php esc_attr_e( 'Snippets with a lower priority number will run before those with a higher number.', 'code-snippets' ); ?>">
372 <label for="snippet_priority"><?php esc_html_e( 'Priority', 'code-snippets' ); ?></label>
373
374 <input name="snippet_priority" type="number" id="snippet_priority" value="<?php echo intval( $snippet->priority ); ?>">
375 </p>
376 <?php
377 }
378
379 /**
380 * Render the snippet scope setting
381 *
382 * @param Code_Snippet $snippet the snippet currently being edited
383 */
384 function render_scope_setting( Code_Snippet $snippet ) {
385
386 $icons = Code_Snippet::get_scope_icons();
387
388 $labels = array(
389 'global' => __( 'Run snippet everywhere', 'code-snippets' ),
390 'admin' => __( 'Only run in administration area', 'code-snippets' ),
391 'front-end' => __( 'Only run on site front-end', 'code-snippets' ),
392 'single-use' => __( 'Only run once', 'code-snippets' ),
393 );
394
395 echo '<h2 class="screen-reader-text">' . esc_html__( 'Scope', 'code-snippets' ) . '</h2><p class="snippet-scope">';
396
397 foreach ( Code_Snippet::get_all_scopes() as $scope ) {
398 printf( '<label><input type="radio" name="snippet_scope" value="%s"', $scope );
399 checked( $scope, $snippet->scope );
400 printf( '> <span class="dashicons dashicons-%s"></span> %s</label>', $icons[ $scope ], esc_html( $labels[ $scope ] ) );
401 }
402
403 echo '</p>';
404 }
405
406 /**
407 * Render the setting for shared network snippets
408 *
409 * @param object $snippet The snippet currently being edited
410 */
411 function render_multisite_sharing_setting( $snippet ) {
412 $shared_snippets = get_site_option( 'shared_network_snippets', array() );
413 ?>
414
415 <div class="snippet-sharing-setting">
416 <h2 class="screen-reader-text"><?php _e( 'Sharing Settings', 'code-snippets' ); ?></h2>
417 <label for="snippet_sharing">
418 <input type="checkbox" name="snippet_sharing"
419 <?php checked( in_array( $snippet->id, $shared_snippets ) ); ?>>
420 <?php esc_html_e( 'Allow this snippet to be activated on individual sites on the network', 'code-snippets' ); ?>
421 </label>
422 </div>
423
424 <?php
425 }
426
427 /**
428 * Retrieve the first error in a snippet's code
429 *
430 * @param $snippet_id
431 *
432 * @return array|bool
433 */
434 private function get_snippet_error( $snippet_id ) {
435
436 if ( ! intval( $snippet_id ) ) {
437 return false;
438 }
439
440 $snippet = get_snippet( intval( $snippet_id ) );
441
442 if ( '' === $snippet->code ) {
443 return false;
444 }
445
446 ob_start();
447 $result = eval( $snippet->code );
448 ob_end_clean();
449
450 if ( false !== $result ) {
451 return false;
452 }
453
454 $error = error_get_last();
455
456 if ( is_null( $error ) ) {
457 return false;
458 }
459
460 return $error;
461 }
462
463 /**
464 * Print the status and error messages
465 */
466 protected function print_messages() {
467
468 if ( ! isset( $_REQUEST['result'] ) ) {
469 return;
470 }
471
472 $result = $_REQUEST['result'];
473
474 if ( 'code-error' === $result ) {
475
476 if ( isset( $_REQUEST['id'] ) && $error = $this->get_snippet_error( $_REQUEST['id'] ) ) {
477
478 printf(
479 '<div id="message" class="error fade"><p>%s</p><p><strong>%s</strong></p></div>',
480 /* translators: %d: line of file where error originated */
481 sprintf( __( 'The snippet has been deactivated due to an error on line %d:', 'code-snippets' ), $error['line'] ),
482 $error['message']
483 );
484
485 } else {
486 echo '<div id="message" class="error fade"><p>', __( 'The snippet has been deactivated due to an error in the code.', 'code-snippets' ), '</p></div>';
487 }
488
489 return;
490 }
491
492 if ( 'save-error' === $result ) {
493 echo '<div id="message" class="error fade"><p>', __( 'An error occurred when saving the snippet.', 'code-snippets' ), '</p></div>';
494
495 return;
496 }
497
498 $messages = array(
499 'added' => __( 'Snippet <strong>added</strong>.', 'code-snippets' ),
500 'updated' => __( 'Snippet <strong>updated</strong>.', 'code-snippets' ),
501 'added-and-activated' => __( 'Snippet <strong>added</strong> and <strong>activated</strong>.', 'code-snippets' ),
502 'updated-and-executed' => __( 'Snippet <strong>added</strong> and <strong>executed</strong>.', 'code-snippets' ),
503 'updated-and-activated' => __( 'Snippet <strong>updated</strong> and <strong>activated</strong>.', 'code-snippets' ),
504 'updated-and-deactivated' => __( 'Snippet <strong>updated</strong> and <strong>deactivated</strong>.', 'code-snippets' ),
505 );
506
507 if ( isset( $messages[ $result ] ) ) {
508 echo '<div id="message" class="updated fade"><p>', $messages[ $result ], '</p></div>';
509 }
510 }
511
512 /**
513 * Enqueue the Tag It library
514 */
515 function enqueue_tagit() {
516 $tagit_version = '2.0';
517 $url = plugin_dir_url( CODE_SNIPPETS_FILE );
518
519 /* Tag It UI */
520 wp_enqueue_script(
521 'code-snippets-tag-it',
522 $url . 'js/min/tag-it.js',
523 array(
524 'jquery-ui-core',
525 'jquery-ui-widget',
526 'jquery-ui-position',
527 'jquery-ui-autocomplete',
528 'jquery-effects-blind',
529 'jquery-effects-highlight',
530 ),
531 $tagit_version
532 );
533
534 wp_enqueue_style(
535 'code-snippets-tag-it',
536 $url . 'css/min/tagit.css',
537 false, $tagit_version
538 );
539 }
540
541 /**
542 * Remove the old CodeMirror version used by the Debug Bar Console plugin
543 * that is messing up the snippet editor
544 */
545 function remove_debug_bar_codemirror() {
546
547 /* Try to discern if we are on the single snippet page as best as we can at this early time */
548 if ( ! is_admin() || 'admin.php' !== $GLOBALS['pagenow'] ) {
549 return;
550 }
551
552 if ( ! isset( $_GET['page'] ) || code_snippets()->get_menu_slug( 'edit' ) !== $_GET['page'] && code_snippets()->get_menu_slug( 'settings' ) ) {
553 return;
554 }
555
556 remove_action( 'debug_bar_enqueue_scripts', 'debug_bar_console_scripts' );
557 }
558 }
559