PluginProbe
Code Snippets / 2.13.3
Code Snippets v2.13.3
4.0.0-beta.2 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 All 65 releases
code-snippets / php / admin-menus / class-edit-menu.php

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

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