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

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