PluginProbe
Code Snippets / 2.14.6
Code Snippets v2.14.6
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.14.6, at php/admin-menus/class-edit-menu.php

804 lines 24.1 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 if ( apply_filters( 'code_snippets/extra_save_buttons', true ) ) {
100 add_action( 'code_snippets/admin/code_editor_toolbar', array( $this, 'render_extra_submit_buttons' ) );
101 }
102
103 if ( apply_filters( 'code_snippets/enable_code_direction', is_rtl() ) ) {
104 add_action( 'code_snippets/admin/code_editor_toolbar', array( $this, 'render_direction_setting' ), 11, 0 );
105 }
106
107 $this->process_actions();
108 }
109
110 /**
111 * Load the data for the snippet currently being edited
112 */
113 public function load_snippet_data() {
114 $edit_id = isset( $_REQUEST['id'] ) && intval( $_REQUEST['id'] ) ? absint( $_REQUEST['id'] ) : 0;
115 $this->snippet = get_snippet( $edit_id );
116 }
117
118 /**
119 * Process data sent from the edit page
120 */
121 private function process_actions() {
122
123 /* Check for a valid nonce */
124 if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'save_snippet' ) ) {
125 return;
126 }
127
128 if ( isset( $_POST['save_snippet'] ) || isset( $_POST['save_snippet_execute'] ) ||
129 isset( $_POST['save_snippet_activate'] ) || isset( $_POST['save_snippet_deactivate'] ) ) {
130 $this->save_posted_snippet();
131 }
132
133 if ( isset( $_POST['snippet_id'] ) ) {
134 $snippet_id = intval( $_POST['snippet_id'] );
135
136 /* Delete the snippet if the button was clicked */
137 if ( isset( $_POST['delete_snippet'] ) ) {
138 delete_snippet( $snippet_id );
139 wp_redirect( add_query_arg( 'result', 'delete', code_snippets()->get_menu_url( 'manage' ) ) );
140 exit;
141 }
142
143 /* Export the snippet if the button was clicked */
144 if ( isset( $_POST['export_snippet'] ) ) {
145 export_snippets( array( $snippet_id ) );
146 }
147
148 /* Download the snippet if the button was clicked */
149 if ( isset( $_POST['download_snippet'] ) ) {
150 download_snippets( array( $snippet_id ) );
151 }
152 }
153 }
154
155 /**
156 * Remove the sharing status from a network snippet
157 *
158 * @param int $snippet_id
159 */
160 private function unshare_network_snippet( $snippet_id ) {
161 $shared_snippets = get_site_option( 'shared_network_snippets', array() );
162
163 if ( ! in_array( $snippet_id, $shared_snippets, true ) ) {
164 return;
165 }
166
167 /* Remove the snippet ID from the array */
168 $shared_snippets = array_diff( $shared_snippets, array( $snippet_id ) );
169 update_site_option( 'shared_network_snippets', array_values( $shared_snippets ) );
170
171 /* Deactivate on all sites */
172 global $wpdb;
173 $sites = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
174 if ( $sites ) {
175 foreach ( $sites as $site ) {
176 switch_to_blog( $site );
177 $active_shared_snippets = get_option( 'active_shared_network_snippets' );
178
179 if ( is_array( $active_shared_snippets ) ) {
180 $active_shared_snippets = array_diff( $active_shared_snippets, array( $snippet_id ) );
181 update_option( 'active_shared_network_snippets', $active_shared_snippets );
182 }
183 }
184
185 restore_current_blog();
186 }
187 }
188
189 private function code_error_callback( $out ) {
190 $error = error_get_last();
191
192 if ( is_null( $error ) ) {
193 return $out;
194 }
195
196 $m = '<h3>' . esc_html__( "Don't Panic", 'code-snippets' ) . '</h3>';
197 /* translators: %d: line where error was produced */
198 $m .= '<p>' . sprintf( esc_html__( 'The code snippet you are trying to save produced a fatal error on line %d:', 'code-snippets' ), intval( $error['line'] ) ) . '</p>';
199 $m .= '<strong>' . esc_html( $error['message'] ) . '</strong>';
200 $m .= '<p>' . esc_html__( 'The previous version of the snippet is unchanged, and the rest of this site should be functioning normally as before.', 'code-snippets' ) . '</p>';
201 $m .= '<p>' . esc_html__( 'Please use the back button in your browser to return to the previous page and try to fix the code error.', 'code-snippets' );
202 $m .= ' ' . esc_html__( '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>';
203
204 return $m;
205 }
206
207 /**
208 * Validate the snippet code before saving to database
209 *
210 * @param Code_Snippet $snippet
211 *
212 * @return bool true if code produces errors
213 */
214 private function test_code( Code_Snippet $snippet ) {
215
216 if ( empty( $snippet->code ) ) {
217 return false;
218 }
219
220 ob_start( array( $this, 'code_error_callback' ) );
221
222 $result = eval( $snippet->code );
223
224 ob_end_clean();
225
226 do_action( 'code_snippets/after_execute_snippet', $snippet->id, $snippet->code, $result );
227
228 return false === $result;
229 }
230
231 /**
232 * Save the posted snippet data to the database and redirect
233 */
234 private function save_posted_snippet() {
235
236 /* Build snippet object from fields with 'snippet_' prefix */
237 $snippet = new Code_Snippet();
238
239 foreach ( $_POST as $field => $value ) {
240 if ( 'snippet_' === substr( $field, 0, 8 ) ) {
241
242 /* Remove the 'snippet_' prefix from field name and set it on the object */
243 $snippet->set_field( substr( $field, 8 ), stripslashes( $value ) );
244 }
245 }
246
247 if ( isset( $_POST['save_snippet_execute'] ) && 'single-use' !== $snippet->scope ) {
248 unset( $_POST['save_snippet_execute'] );
249 $_POST['save_snippet'] = 'yes';
250 }
251
252 /* Activate or deactivate the snippet before saving if we clicked the button */
253
254 if ( isset( $_POST['save_snippet_execute'] ) ) {
255 $snippet->active = 1;
256 } elseif ( isset( $_POST['snippet_sharing'] ) && 'on' === $_POST['snippet_sharing'] ) {
257 // Shared network snippets cannot be network activated
258 $snippet->active = 0;
259 unset( $_POST['save_snippet_activate'], $_POST['save_snippet_deactivate'] );
260 } elseif ( isset( $_POST['save_snippet_activate'] ) ) {
261 $snippet->active = 1;
262 } elseif ( isset( $_POST['save_snippet_deactivate'] ) ) {
263 $snippet->active = 0;
264 }
265
266 /* Deactivate snippet if code contains errors */
267 if ( $snippet->active && 'single-use' !== $snippet->scope ) {
268 $validator = new Code_Snippets_Validator( $snippet->code );
269 $code_error = $validator->validate();
270
271 if ( ! $code_error ) {
272 $code_error = $this->test_code( $snippet );
273 }
274
275 if ( $code_error ) {
276 $snippet->active = 0;
277 }
278 }
279
280 /* Save the snippet to the database */
281 $snippet_id = save_snippet( $snippet );
282
283 /* Update the shared network snippets if necessary */
284 if ( $snippet_id && is_network_admin() ) {
285
286 if ( isset( $_POST['snippet_sharing'] ) && 'on' === $_POST['snippet_sharing'] ) {
287 $shared_snippets = get_site_option( 'shared_network_snippets', array() );
288
289 /* Add the snippet ID to the array if it isn't already */
290 if ( ! in_array( $snippet_id, $shared_snippets, true ) ) {
291 $shared_snippets[] = $snippet_id;
292 update_site_option( 'shared_network_snippets', array_values( $shared_snippets ) );
293 }
294 } else {
295 $this->unshare_network_snippet( $snippet_id );
296 }
297 }
298
299 /* If the saved snippet ID is invalid, display an error message */
300 if ( ! $snippet_id || $snippet_id < 1 ) {
301 /* An error occurred */
302 wp_redirect( add_query_arg( 'result', 'save-error', code_snippets()->get_menu_url( 'add' ) ) );
303 exit;
304 }
305
306 /* Display message if a parse error occurred */
307 if ( isset( $code_error ) && $code_error ) {
308 wp_redirect(
309 add_query_arg(
310 array(
311 'id' => $snippet_id,
312 'result' => 'code-error',
313 ),
314 code_snippets()->get_menu_url( 'edit' )
315 )
316 );
317 exit;
318 }
319
320 /* Set the result depending on if the snippet was just added */
321 $result = isset( $_POST['snippet_id'] ) ? 'updated' : 'added';
322
323 /* Append a suffix if the snippet was activated or deactivated */
324 if ( isset( $_POST['save_snippet_activate'] ) ) {
325 $result .= '-and-activated';
326 } elseif ( isset( $_POST['save_snippet_deactivate'] ) ) {
327 $result .= '-and-deactivated';
328 } elseif ( isset( $_POST['save_snippet_execute'] ) ) {
329 $result .= '-and-executed';
330 }
331
332 /* Redirect to edit snippet page */
333 $redirect_uri = add_query_arg(
334 array(
335 'id' => $snippet_id,
336 'result' => $result,
337 ),
338 code_snippets()->get_menu_url( 'edit' )
339 );
340
341 wp_redirect( esc_url_raw( $redirect_uri ) );
342 exit;
343 }
344
345 /**
346 * Add a description editor to the single snippet page
347 *
348 * @param Code_Snippet $snippet The snippet being used for this page
349 */
350 public function render_description_editor( Code_Snippet $snippet ) {
351 $settings = code_snippets_get_settings();
352 $settings = $settings['description_editor'];
353
354 /* Hack to remove space between heading and editor tabs */
355 $inline_heading = ! $settings['media_buttons'] && 'false' !== get_user_option( 'rich_editing' );
356
357 echo '<h2><label for="snippet_description">', $inline_heading ? '<div>' : '';
358 esc_html_e( 'Description', 'code-snippets' );
359 echo $inline_heading ? '</div>' : '', '</label></h2>';
360
361 remove_editor_styles(); // stop custom theme styling interfering with the editor
362
363 wp_editor(
364 $snippet->desc,
365 'description',
366 apply_filters( 'code_snippets/admin/description_editor_settings',
367 array(
368 'textarea_name' => 'snippet_description',
369 'textarea_rows' => $settings['rows'],
370 'teeny' => ! $settings['use_full_mce'],
371 'media_buttons' => $settings['media_buttons'],
372 )
373 )
374 );
375 }
376
377 /**
378 * Render the interface for editing snippet tags
379 *
380 * @param Code_Snippet $snippet the snippet currently being edited
381 */
382 public function render_tags_editor( Code_Snippet $snippet ) {
383
384 ?>
385 <h2 style="margin: 25px 0 10px;">
386 <label for="snippet_tags" style="cursor: auto;">
387 <?php esc_html_e( 'Tags', 'code-snippets' ); ?>
388 </label>
389 </h2>
390
391 <input type="text" id="snippet_tags" name="snippet_tags" style="width: 100%;"
392 placeholder="<?php esc_html_e( 'Enter a list of tags; separated by commas', 'code-snippets' ); ?>"
393 value="<?php echo esc_attr( $snippet->tags_list ); ?>"/>
394 <?php
395 }
396
397 /**
398 * Render the snippet priority setting
399 *
400 * @param Code_Snippet $snippet the snippet currently being edited
401 */
402 public function render_priority_setting( Code_Snippet $snippet ) {
403 ?>
404 <p class="snippet-priority"
405 title="<?php esc_attr_e( 'Snippets with a lower priority number will run before those with a higher number.', 'code-snippets' ); ?>">
406 <label for="snippet_priority"><?php esc_html_e( 'Priority', 'code-snippets' ); ?></label>
407
408 <input name="snippet_priority" type="number" id="snippet_priority"
409 value="<?php echo esc_attr( $snippet->priority ); ?>">
410 </p>
411 <?php
412 }
413
414 /**
415 * Render the snippet scope setting
416 *
417 * @param Code_Snippet $snippet the snippet currently being edited
418 */
419 public function render_scope_setting( Code_Snippet $snippet ) {
420
421 $icons = Code_Snippet::get_scope_icons();
422
423 $labels = array(
424 'global' => __( 'Run snippet everywhere', 'code-snippets' ),
425 'admin' => __( 'Only run in administration area', 'code-snippets' ),
426 'front-end' => __( 'Only run on site front-end', 'code-snippets' ),
427 'single-use' => __( 'Only run once', 'code-snippets' ),
428 );
429
430 echo '<h2 class="screen-reader-text">' . esc_html__( 'Scope', 'code-snippets' ) . '</h2><p class="snippet-scope">';
431
432 foreach ( Code_Snippet::get_all_scopes() as $scope ) {
433 printf( '<label><input type="radio" name="snippet_scope" value="%s"', esc_attr( $scope ) );
434 checked( $scope, $snippet->scope );
435 printf( '> <span class="dashicons dashicons-%s"></span> %s</label>', esc_attr( $icons[ $scope ] ), esc_html( $labels[ $scope ] ) );
436 }
437
438 echo '</p>';
439 }
440
441 /**
442 * Render the setting for shared network snippets
443 *
444 * @param object $snippet The snippet currently being edited
445 */
446 public function render_multisite_sharing_setting( $snippet ) {
447 $shared_snippets = get_site_option( 'shared_network_snippets', array() );
448 ?>
449
450 <div class="snippet-sharing-setting">
451 <h2 class="screen-reader-text"><?php esc_html_e( 'Sharing Settings', 'code-snippets' ); ?></h2>
452 <label for="snippet_sharing">
453 <input type="checkbox" name="snippet_sharing"
454 <?php checked( in_array( $snippet->id, $shared_snippets, true ) ); ?>>
455 <?php esc_html_e( 'Allow this snippet to be activated on individual sites on the network', 'code-snippets' ); ?>
456 </label>
457 </div>
458
459 <?php
460 }
461
462 /**
463 * Render additional save buttons above the snippet editor.
464 *
465 * @param Code_Snippet $snippet Snippet currently being edited.
466 */
467 public function render_extra_submit_buttons( Code_Snippet $snippet ) {
468 $actions['save_snippet'] = array(
469 __( 'Save Changes', 'code-snippets' ),
470 __( 'Save Snippet', 'code-snippets' ),
471 );
472
473 if ( 'single-use' === $snippet->scope ) {
474 $actions['save_snippet_execute'] = array(
475 __( 'Execute Once', 'code-snippets' ),
476 __( 'Save Snippet and Execute Once', 'code-snippets' ),
477 );
478
479 } elseif ( ! $snippet->shared_network || ! is_network_admin() ) {
480
481 if ( $snippet->active ) {
482 $actions['save_snippet_deactivate'] = array(
483 __( 'Deactivate', 'code-snippets' ),
484 __( 'Save Snippet and Deactivate', 'code-snippets' ),
485 );
486
487 } else {
488 $actions['save_snippet_activate'] = array(
489 __( 'Activate', 'code-snippets' ),
490 __( 'Save Snippet and Activate', 'code-snippets' ),
491 );
492 }
493 }
494
495 foreach ( $actions as $action => $labels ) {
496 $other_attributes = array(
497 'title' => $labels[1],
498 'id' => $action . '_extra',
499 );
500 submit_button( $labels[0], 'secondary small', $action, false, $other_attributes );
501 }
502 }
503
504 /**
505 * Render a control for changing the code editor text direction
506 */
507 public function render_direction_setting() {
508 ?>
509 <label class="screen-reader-text" for="snippet-code-direction">
510 <?php esc_html_e( 'Code Direction', 'code-snippets' ); ?>
511 </label>
512 <select id="snippet-code-direction">
513 <option value="ltr"><?php esc_html_e( 'LTR', 'code-snippets' ); ?></option>
514 <option value="rtl"><?php esc_html_e( 'RTL', 'code-snippets' ); ?></option>
515 </select>
516 <?php
517 }
518
519 /**
520 * Retrieve the first error in a snippet's code
521 *
522 * @param $snippet_id
523 *
524 * @return array|bool
525 */
526 private function get_snippet_error( $snippet_id ) {
527
528 if ( ! intval( $snippet_id ) ) {
529 return false;
530 }
531
532 $snippet = get_snippet( intval( $snippet_id ) );
533
534 if ( '' === $snippet->code ) {
535 return false;
536 }
537
538 $validator = new Code_Snippets_Validator( $snippet->code );
539 $error = $validator->validate();
540
541 if ( $error ) {
542 return $error;
543 }
544
545 ob_start();
546 $result = eval( $snippet->code );
547 ob_end_clean();
548
549 if ( false !== $result ) {
550 return false;
551 }
552
553 $error = error_get_last();
554
555 if ( is_null( $error ) ) {
556 return false;
557 }
558
559 return $error;
560 }
561
562 /**
563 * Print the status and error messages
564 */
565 protected function print_messages() {
566
567 if ( ! isset( $_REQUEST['result'] ) ) {
568 return;
569 }
570
571 $result = sanitize_key( $_REQUEST['result'] );
572
573 if ( 'code-error' === $result ) {
574 $error = isset( $_REQUEST['id'] ) ? $this->get_snippet_error( intval( $_REQUEST['id'] ) ) : false;
575
576 if ( $error ) {
577 printf(
578 '<div id="message" class="error fade"><p>%s</p><p><strong>%s</strong></p></div>',
579 /* translators: %d: line of file where error originated */
580 sprintf( esc_html__( 'The snippet has been deactivated due to an error on line %d:', 'code-snippets' ), intval( $error['line'] ) ),
581 esc_html( $error['message'] )
582 );
583
584 } else {
585 echo '<div id="message" class="error fade"><p>', esc_html__( 'The snippet has been deactivated due to an error in the code.', 'code-snippets' ), '</p></div>';
586 }
587
588 return;
589 }
590
591 if ( 'save-error' === $result ) {
592 echo '<div id="message" class="error fade"><p>', esc_html__( 'An error occurred when saving the snippet.', 'code-snippets' ), '</p></div>';
593
594 return;
595 }
596
597 $messages = array(
598 'added' => __( 'Snippet <strong>added</strong>.', 'code-snippets' ),
599 'updated' => __( 'Snippet <strong>updated</strong>.', 'code-snippets' ),
600 'added-and-activated' => __( 'Snippet <strong>added</strong> and <strong>activated</strong>.', 'code-snippets' ),
601 'updated-and-executed' => __( 'Snippet <strong>added</strong> and <strong>executed</strong>.', 'code-snippets' ),
602 'updated-and-activated' => __( 'Snippet <strong>updated</strong> and <strong>activated</strong>.', 'code-snippets' ),
603 'updated-and-deactivated' => __( 'Snippet <strong>updated</strong> and <strong>deactivated</strong>.', 'code-snippets' ),
604 );
605
606 if ( isset( $messages[ $result ] ) ) {
607 echo '<div id="message" class="updated fade"><p>', wp_kses( $messages[ $result ], array( 'strong' => array() ) ), '</p></div>';
608 }
609 }
610
611 /**
612 * Enqueue assets for the edit menu
613 */
614 public function enqueue_assets() {
615 $plugin = code_snippets();
616 $rtl = is_rtl() ? '-rtl' : '';
617
618 code_snippets_enqueue_editor();
619
620 wp_enqueue_style(
621 'code-snippets-edit',
622 plugins_url( "css/min/edit{$rtl}.css", $plugin->file ),
623 array(),
624 $plugin->version
625 );
626
627 wp_enqueue_script(
628 'code-snippets-edit-menu',
629 plugins_url( 'js/min/edit.js', $plugin->file ),
630 array(),
631 $plugin->version,
632 true
633 );
634
635 $atts = code_snippets_get_editor_atts( array(), true );
636 $inline_script = 'var code_snippets_editor_atts = ' . $atts . ';';
637
638 wp_add_inline_script( 'code-snippets-edit-menu', $inline_script, 'before' );
639
640 if ( code_snippets_get_setting( 'general', 'enable_tags' ) ) {
641
642 wp_enqueue_script(
643 'code-snippets-edit-menu-tags',
644 plugins_url( 'js/min/edit-tags.js', $plugin->file ),
645 array(),
646 $plugin->version,
647 true
648 );
649
650 $options = apply_filters( 'code_snippets/tag_editor_options',
651 array(
652 'allow_spaces' => true,
653 'available_tags' => get_all_snippet_tags(),
654 )
655 );
656
657 $inline_script = 'var code_snippets_tags = ' . wp_json_encode( $options ) . ';';
658 wp_add_inline_script( 'code-snippets-edit-menu-tags', $inline_script, 'before' );
659 }
660 }
661
662 /**
663 * Remove the old CodeMirror version used by the Debug Bar Console plugin
664 * that is messing up the snippet editor
665 */
666 public function remove_debug_bar_codemirror() {
667
668 /* Try to discern if we are on the single snippet page as best as we can at this early time */
669 if ( ! is_admin() || 'admin.php' !== $GLOBALS['pagenow'] ) {
670 return;
671 }
672
673 if ( ! isset( $_GET['page'] ) || code_snippets()->get_menu_slug( 'edit' ) !== $_GET['page'] && code_snippets()->get_menu_slug( 'settings' ) !== $_GET['page'] ) {
674 return;
675 }
676
677 remove_action( 'debug_bar_enqueue_scripts', 'debug_bar_console_scripts' );
678 }
679
680 /**
681 * Retrieve a list of submit actions for a given snippet
682 *
683 * @param Code_Snippet $snippet
684 * @param bool $extra_actions
685 *
686 * @return array
687 */
688 public function get_actions_list( $snippet, $extra_actions = true ) {
689 $actions = array(
690 'save_snippet' => __( 'Save Changes', 'code-snippets' ),
691 );
692
693 if ( 'single-use' === $snippet->scope ) {
694 $actions['save_snippet_execute'] = __( 'Save Changes and Execute Once', 'code-snippets' );
695
696 } elseif ( ! $snippet->shared_network || ! is_network_admin() ) {
697
698 if ( $snippet->active ) {
699 $actions['save_snippet_deactivate'] = __( 'Save Changes and Deactivate', 'code-snippets' );
700 } else {
701 $actions['save_snippet_activate'] = __( 'Save Changes and Activate', 'code-snippets' );
702 }
703 }
704
705 // Make the 'Save and Activate' button the default if the setting is enabled
706 if ( ! $snippet->active && 'single-use' !== $snippet->scope &&
707 code_snippets_get_setting( 'general', 'activate_by_default' ) ) {
708 $actions = array_reverse( $actions );
709 }
710
711 if ( $extra_actions && 0 !== $snippet->id ) {
712
713 if ( apply_filters( 'code_snippets/enable_downloads', true ) ) {
714 $actions['download_snippet'] = __( 'Download', 'code-snippets' );
715 }
716
717 $actions['export_snippet'] = __( 'Export', 'code-snippets' );
718 $actions['delete_snippet'] = __( 'Delete', 'code-snippets' );
719 }
720
721 return apply_filters( 'code_snippets/admin/submit_actions', $actions, $snippet, $extra_actions );
722 }
723
724 /**
725 * Render the submit buttons for a code snippet
726 *
727 * @param Code_Snippet $snippet
728 * @param string $size
729 * @param bool $extra_actions
730 */
731 public function render_submit_buttons( $snippet, $size = '', $extra_actions = true ) {
732
733 $actions = $this->get_actions_list( $snippet, $extra_actions );
734 $type = 'primary';
735 $size = $size ? ' ' . $size : '';
736
737 foreach ( $actions as $action => $label ) {
738 $other = null;
739
740 if ( 'delete_snippet' === $action ) {
741 $other = sprintf(
742 'onclick="%s"',
743 esc_js(
744 sprintf(
745 'return confirm("%s");',
746 esc_html__( 'You are about to permanently delete this snippet.', 'code-snippets' ) . "\n" .
747 esc_html__( "'Cancel' to stop, 'OK' to delete.", 'code-snippets' )
748 )
749 )
750 );
751 }
752
753 submit_button( $label, $type . $size, $action, false, $other );
754
755 if ( 'primary' === $type ) {
756 $type = 'secondary';
757 }
758 }
759 }
760
761 /**
762 * Render a keyboard shortcut as HTML.
763 *
764 * @param array|string $modifiers Modifier keys. Can be 'Cmd', 'Ctrl', 'Shift', 'Option', 'Alt'.
765 * @param string $key Keyboard key.
766 *
767 * @return void
768 */
769 protected function render_keyboard_shortcut( $modifiers, $key ) {
770 static $keys = null;
771
772 if ( is_null( $keys ) ) {
773 $keys = array(
774 'Cmd' => _x( 'Cmd', 'keyboard key', 'code-snippets' ),
775 'Ctrl' => _x( 'Ctrl', 'keyboard key', 'code-snippets' ),
776 'Shift' => _x( 'Shift', 'keyboard key', 'code-snippets' ),
777 'Option' => _x( 'Option', 'keyboard key', 'code-snippets' ),
778 'Alt' => _x( 'Alt', 'keyboard key', 'code-snippets' ),
779 'F' => _x( 'F', 'keyboard key', 'code-snippets' ),
780 'G' => _x( 'G', 'keyboard key', 'code-snippets' ),
781 'R' => _x( 'R', 'keyboard key', 'code-snippets' ),
782 'S' => _x( 'S', 'keyboard key', 'code-snippets' ),
783 );
784 }
785
786 if ( ! is_array( $modifiers ) ) {
787 $modifiers = array( $modifiers );
788 }
789
790 foreach ( $modifiers as $modifier ) {
791 if ( 'Ctrl' === $modifier || 'Cmd' === $modifier ) {
792 echo '<kbd class="pc-key">', esc_html( $keys['Ctrl'] ), '</kbd>';
793 echo '<kbd class="mac-key">', esc_html( $keys['Cmd'] ), '</kbd>&hyphen;';
794 } elseif ( 'Option' === $modifier ) {
795 echo '<span class="mac-key"><kbd class="mac-key">', esc_html( $keys['Option'] ), '</kbd>&hyphen;</span>';
796 } else {
797 echo '<kbd>', esc_html( $keys[ $modifier ] ), '</kbd>&hyphen;';
798 }
799 }
800
801 echo '<kbd>', esc_html( $keys[ $key ] ), '</kbd>';
802 }
803 }
804