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

554 lines 16.6 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 * @param int $snippet_id
127 */
128 private function unshare_network_snippet( $snippet_id ) {
129 $shared_snippets = get_site_option( 'shared_network_snippets', array() );
130
131 if ( ! in_array( $snippet_id, $shared_snippets ) ) {
132 return;
133 }
134
135 /* Remove the snippet ID from the array */
136 $shared_snippets = array_diff( $shared_snippets, array( $snippet_id ) );
137 update_site_option( 'shared_network_snippets', array_values( $shared_snippets ) );
138
139 /* Deactivate on all sites */
140 global $wpdb;
141 if ( $sites = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ) ) {
142
143 foreach ( $sites as $site ) {
144 switch_to_blog( $site );
145 $active_shared_snippets = get_option( 'active_shared_network_snippets' );
146
147 if ( is_array( $active_shared_snippets ) ) {
148 $active_shared_snippets = array_diff( $active_shared_snippets, array( $snippet_id ) );
149 update_option( 'active_shared_network_snippets', $active_shared_snippets );
150 }
151 }
152
153 restore_current_blog();
154 }
155 }
156
157 private function code_error_callback( $out ) {
158 $error = error_get_last();
159
160 if ( is_null( $error ) ) {
161 return $out;
162 }
163
164 $m = '<h3>' . __( "Don't Panic", 'code-snippets' ) . '</h3>';
165 $m .= '<p>' . sprintf( __( 'The code snippet you are trying to save produced a fatal error on line %d:', 'code-snippets' ), $error['line'] ) . '</p>';
166 $m .= '<strong>' . $error['message'] . '</strong>';
167 $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>';
168 $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' );
169 $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>';
170
171 return $m;
172 }
173
174 /**
175 * Validate the snippet code before saving to database
176 *
177 * @param Code_Snippet $snippet
178 *
179 * @return bool true if code produces errors
180 */
181 private function validate_code( Code_Snippet $snippet ) {
182
183 if ( empty( $snippet->code ) ) {
184 return false;
185 }
186
187 ob_start( array( $this, 'code_error_callback' ) );
188
189 $result = eval( $snippet->code );
190
191 ob_end_clean();
192
193 do_action( 'code_snippets/after_execute_snippet', $snippet->id, $snippet->code, $result );
194
195 return false === $result;
196 }
197
198 /**
199 * Save the posted snippet data to the database and redirect
200 */
201 private function save_posted_snippet() {
202
203 /* Build snippet object from fields with 'snippet_' prefix */
204 $snippet = new Code_Snippet();
205
206 foreach ( $_POST as $field => $value ) {
207 if ( 'snippet_' === substr( $field, 0, 8 ) ) {
208
209 /* Remove the 'snippet_' prefix from field name and set it on the object */
210 $snippet->set_field( substr( $field, 8 ), stripslashes( $value ) );
211 }
212 }
213
214 if ( isset( $_POST['save_snippet_execute'] ) && 'single-use' !== $snippet->scope ) {
215 unset( $_POST['save_snippet_execute'] );
216 $_POST['save_snippet'] = 'yes';
217 }
218
219 /* Activate or deactivate the snippet before saving if we clicked the button */
220
221 if ( isset( $_POST['save_snippet_execute'] ) ) {
222 $snippet->active = 1;
223 } elseif ( isset( $_POST['snippet_sharing'] ) && 'on' === $_POST['snippet_sharing'] ) {
224 // Shared network snippets cannot be network activated
225 $snippet->active = 0;
226 unset( $_POST['save_snippet_activate'], $_POST['save_snippet_deactivate'] );
227 } elseif ( isset( $_POST['save_snippet_activate'] ) ) {
228 $snippet->active = 1;
229 } elseif ( isset( $_POST['save_snippet_deactivate'] ) ) {
230 $snippet->active = 0;
231 }
232
233 /* Deactivate snippet if code contains errors */
234 if ( $snippet->active && 'single-use' !== $snippet->scope ) {
235 if ( $code_error = $this->validate_code( $snippet ) ) {
236 $snippet->active = 0;
237 }
238 }
239
240 /* Save the snippet to the database */
241 $snippet_id = save_snippet( $snippet );
242
243 /* Update the shared network snippets if necessary */
244 if ( $snippet_id && is_network_admin() ) {
245
246 if ( isset( $_POST['snippet_sharing'] ) && 'on' === $_POST['snippet_sharing'] ) {
247 $shared_snippets = get_site_option( 'shared_network_snippets', array() );
248
249 /* Add the snippet ID to the array if it isn't already */
250 if ( ! in_array( $snippet_id, $shared_snippets ) ) {
251 $shared_snippets[] = $snippet_id;
252 update_site_option( 'shared_network_snippets', array_values( $shared_snippets ) );
253 }
254 } else {
255 $this->unshare_network_snippet( $snippet_id );
256 }
257 }
258
259 /* If the saved snippet ID is invalid, display an error message */
260 if ( ! $snippet_id || $snippet_id < 1 ) {
261 /* An error occurred */
262 wp_redirect( add_query_arg( 'result', 'save-error', code_snippets()->get_menu_url( 'add' ) ) );
263 exit;
264 }
265
266 /* Display message if a parse error occurred */
267 if ( isset( $code_error ) && $code_error ) {
268 wp_redirect( add_query_arg(
269 array( 'id' => $snippet_id, 'result' => 'code-error' ),
270 code_snippets()->get_menu_url( 'edit' )
271 ) );
272 exit;
273 }
274
275 /* Set the result depending on if the snippet was just added */
276 $result = isset( $_POST['snippet_id'] ) ? 'updated' : 'added';
277
278 /* Append a suffix if the snippet was activated or deactivated */
279 if ( isset( $_POST['save_snippet_activate'] ) ) {
280 $result .= '-and-activated';
281 } elseif ( isset( $_POST['save_snippet_deactivate'] ) ) {
282 $result .= '-and-deactivated';
283 } elseif ( isset( $_POST['save_snippet_execute'] ) ) {
284 $result .= '-and-executed';
285 }
286
287 /* Redirect to edit snippet page */
288 $redirect_uri = add_query_arg(
289 array( 'id' => $snippet_id, 'result' => $result ),
290 code_snippets()->get_menu_url( 'edit' )
291 );
292
293 if ( isset( $_POST['snippet_editor_cursor'] ) ) {
294 $redirect_uri = add_query_arg( 'cursor', $_POST['snippet_editor_cursor'], $redirect_uri );
295 }
296
297 wp_redirect( esc_url_raw( $redirect_uri ) );
298 exit;
299 }
300
301 /**
302 * Add a description editor to the single snippet page
303 *
304 * @param Code_Snippet $snippet The snippet being used for this page
305 */
306 function render_description_editor( Code_Snippet $snippet ) {
307 $settings = code_snippets_get_settings();
308 $settings = $settings['description_editor'];
309 $heading = __( 'Description', 'code-snippets' );
310
311 /* Hack to remove space between heading and editor tabs */
312 if ( ! $settings['media_buttons'] && 'false' !== get_user_option( 'rich_editing' ) ) {
313 $heading = "<div>$heading</div>";
314 }
315
316 echo '<h2><label for="snippet_description">', $heading, '</label></h2>';
317
318 remove_editor_styles(); // stop custom theme styling interfering with the editor
319
320 wp_editor(
321 $snippet->desc,
322 'description',
323 apply_filters( 'code_snippets/admin/description_editor_settings', array(
324 'textarea_name' => 'snippet_description',
325 'textarea_rows' => $settings['rows'],
326 'teeny' => ! $settings['use_full_mce'],
327 'media_buttons' => $settings['media_buttons'],
328 ) )
329 );
330 }
331
332 /**
333 * Render the interface for editing snippet tags
334 *
335 * @param Code_Snippet $snippet the snippet currently being edited
336 */
337 function render_tags_editor( Code_Snippet $snippet ) {
338
339 ?>
340 <h2 style="margin: 25px 0 10px;">
341 <label for="snippet_tags" style="cursor: auto;">
342 <?php esc_html_e( 'Tags', 'code-snippets' ); ?>
343 </label>
344 </h2>
345
346 <input type="text" id="snippet_tags" name="snippet_tags" style="width: 100%;"
347 placeholder="<?php esc_html_e( 'Enter a list of tags; separated by commas', 'code-snippets' ); ?>"
348 value="<?php echo esc_attr( $snippet->tags_list ); ?>" />
349
350 <script type="text/javascript">
351 jQuery('#snippet_tags').tagit({
352 availableTags: <?php echo wp_json_encode( get_all_snippet_tags() ); ?>,
353 allowSpaces: true,
354 removeConfirmation: true,
355 showAutocompleteOnFocus: true,
356 });
357 </script>
358 <?php
359 }
360
361 /**
362 * Render the snippet priority setting
363 *
364 * @param Code_Snippet $snippet the snippet currently being edited
365 */
366 public function render_priority_setting( Code_Snippet $snippet ) {
367 ?>
368 <p class="snippet-priority"
369 title="<?php esc_attr_e( 'Snippets with a lower priority number will run before those with a higher number.', 'code-snippets' ); ?>">
370 <label for="snippet_priority"><?php esc_html_e( 'Priority', 'code-snippets' ); ?></label>
371
372 <input name="snippet_priority" type="number" id="snippet_priority" value="<?php echo intval( $snippet->priority ); ?>">
373 </p>
374 <?php
375 }
376
377 /**
378 * Render the snippet scope setting
379 *
380 * @param Code_Snippet $snippet the snippet currently being edited
381 */
382 function render_scope_setting( Code_Snippet $snippet ) {
383
384 $icons = Code_Snippet::get_scope_icons();
385
386 $labels = array(
387 'global' => __( 'Run snippet everywhere', 'code-snippets' ),
388 'admin' => __( 'Only run in administration area', 'code-snippets' ),
389 'front-end' => __( 'Only run on site front-end', 'code-snippets' ),
390 'single-use' => __( 'Only run once', 'code-snippets' ),
391 );
392
393 echo '<h2 class="screen-reader-text">' . esc_html__( 'Scope', 'code-snippets' ) . '</h2><p class="snippet-scope">';
394
395 foreach ( Code_Snippet::get_all_scopes() as $scope ) {
396 printf( '<label><input type="radio" name="snippet_scope" value="%s"', $scope );
397 checked( $scope, $snippet->scope );
398 printf( '> <span class="dashicons dashicons-%s"></span> %s</label>', $icons[ $scope ], esc_html( $labels[ $scope ] ) );
399 }
400
401 echo '</p>';
402 }
403
404 /**
405 * Render the setting for shared network snippets
406 * @param object $snippet The snippet currently being edited
407 */
408 function render_multisite_sharing_setting( $snippet ) {
409 $shared_snippets = get_site_option( 'shared_network_snippets', array() );
410 ?>
411
412 <div class="snippet-sharing-setting">
413 <h2 class="screen-reader-text"><?php _e( 'Sharing Settings', 'code-snippets' ); ?></h2>
414 <label for="snippet_sharing">
415 <input type="checkbox" name="snippet_sharing"
416 <?php checked( in_array( $snippet->id, $shared_snippets ) ); ?>>
417 <?php esc_html_e( 'Allow this snippet to be activated on individual sites on the network', 'code-snippets' ); ?>
418 </label>
419 </div>
420
421 <?php
422 }
423
424 /**
425 * Retrieve the first error in a snippet's code
426 *
427 * @param $snippet_id
428 *
429 * @return array|bool
430 */
431 private function get_snippet_error( $snippet_id ) {
432
433 if ( ! intval( $snippet_id ) ) {
434 return false;
435 }
436
437 $snippet = get_snippet( intval( $snippet_id ) );
438
439 if ( '' === $snippet->code ) {
440 return false;
441 }
442
443 ob_start();
444 $result = eval( $snippet->code );
445 ob_end_clean();
446
447 if ( false !== $result ) {
448 return false;
449 }
450
451 $error = error_get_last();
452
453 if ( is_null( $error ) ) {
454 return false;
455 }
456
457 return $error;
458 }
459
460 /**
461 * Print the status and error messages
462 */
463 protected function print_messages() {
464
465 if ( ! isset( $_REQUEST['result'] ) ) {
466 return;
467 }
468
469 $result = $_REQUEST['result'];
470
471 if ( 'code-error' === $result ) {
472
473 if ( isset( $_REQUEST['id'] ) && $error = $this->get_snippet_error( $_REQUEST['id'] ) ) {
474
475 printf(
476 '<div id="message" class="error fade"><p>%s</p><p><strong>%s</strong></p></div>',
477 sprintf( __( 'The snippet has been deactivated due to an error on line %d:', 'code-snippets' ), $error['line'] ),
478 $error['message']
479 );
480
481 } else {
482 echo '<div id="message" class="error fade"><p>', __( 'The snippet has been deactivated due to an error in the code.', 'code-snippets' ), '</p></div>';
483 }
484
485 return;
486 }
487
488 if ( 'save-error' === $result ) {
489 echo '<div id="message" class="error fade"><p>', __( 'An error occurred when saving the snippet.', 'code-snippets' ), '</p></div>';
490 return;
491 }
492
493 $messages = array(
494 'added' => __( 'Snippet <strong>added</strong>.', 'code-snippets' ),
495 'updated' => __( 'Snippet <strong>updated</strong>.', 'code-snippets' ),
496 'added-and-activated' => __( 'Snippet <strong>added</strong> and <strong>activated</strong>.', 'code-snippets' ),
497 'updated-and-executed' => __( 'Snippet <strong>added</strong> and <strong>executed</strong>.', 'code-snippets' ),
498 'updated-and-activated' => __( 'Snippet <strong>updated</strong> and <strong>activated</strong>.', 'code-snippets' ),
499 'updated-and-deactivated' => __( 'Snippet <strong>updated</strong> and <strong>deactivated</strong>.', 'code-snippets' ),
500 );
501
502 if ( isset( $messages[ $result ] ) ) {
503 echo '<div id="message" class="updated fade"><p>', $messages[ $result ], '</p></div>';
504 }
505 }
506
507 /**
508 * Enqueue the Tag It library
509 */
510 function enqueue_tagit() {
511 $tagit_version = '2.0';
512 $url = plugin_dir_url( CODE_SNIPPETS_FILE );
513
514 /* Tag It UI */
515 wp_enqueue_script(
516 'code-snippets-tag-it',
517 $url . 'js/min/tag-it.js',
518 array(
519 'jquery-ui-core',
520 'jquery-ui-widget',
521 'jquery-ui-position',
522 'jquery-ui-autocomplete',
523 'jquery-effects-blind',
524 'jquery-effects-highlight',
525 ),
526 $tagit_version
527 );
528
529 wp_enqueue_style(
530 'code-snippets-tag-it',
531 $url . 'css/min/tagit.css',
532 false, $tagit_version
533 );
534 }
535
536 /**
537 * Remove the old CodeMirror version used by the Debug Bar Console plugin
538 * that is messing up the snippet editor
539 */
540 function remove_debug_bar_codemirror() {
541
542 /* Try to discern if we are on the single snippet page as best as we can at this early time */
543 if ( ! is_admin() || 'admin.php' !== $GLOBALS['pagenow'] ) {
544 return;
545 }
546
547 if ( ! isset( $_GET['page'] ) || code_snippets()->get_menu_slug( 'edit' ) !== $_GET['page'] && code_snippets()->get_menu_slug( 'settings' ) ) {
548 return;
549 }
550
551 remove_action( 'debug_bar_enqueue_scripts', 'debug_bar_console_scripts' );
552 }
553 }
554