| 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, true ) ) { |
| 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 test_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 |
$validator = new Code_Snippets_Validator( $snippet->code ); |
| 260 |
$code_error = $validator->validate(); |
| 261 |
|
| 262 |
if ( ! $code_error ) { |
| 263 |
$code_error = $this->test_code( $snippet ); |
| 264 |
} |
| 265 |
|
| 266 |
if ( $code_error ) { |
| 267 |
$snippet->active = 0; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
/* Save the snippet to the database */ |
| 272 |
$snippet_id = save_snippet( $snippet ); |
| 273 |
|
| 274 |
/* Update the shared network snippets if necessary */ |
| 275 |
if ( $snippet_id && is_network_admin() ) { |
| 276 |
|
| 277 |
if ( isset( $_POST['snippet_sharing'] ) && 'on' === $_POST['snippet_sharing'] ) { |
| 278 |
$shared_snippets = get_site_option( 'shared_network_snippets', array() ); |
| 279 |
|
| 280 |
/* Add the snippet ID to the array if it isn't already */ |
| 281 |
if ( ! in_array( $snippet_id, $shared_snippets, true ) ) { |
| 282 |
$shared_snippets[] = $snippet_id; |
| 283 |
update_site_option( 'shared_network_snippets', array_values( $shared_snippets ) ); |
| 284 |
} |
| 285 |
} else { |
| 286 |
$this->unshare_network_snippet( $snippet_id ); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
/* If the saved snippet ID is invalid, display an error message */ |
| 291 |
if ( ! $snippet_id || $snippet_id < 1 ) { |
| 292 |
/* An error occurred */ |
| 293 |
wp_redirect( add_query_arg( 'result', 'save-error', code_snippets()->get_menu_url( 'add' ) ) ); |
| 294 |
exit; |
| 295 |
} |
| 296 |
|
| 297 |
/* Display message if a parse error occurred */ |
| 298 |
if ( isset( $code_error ) && $code_error ) { |
| 299 |
wp_redirect( add_query_arg( |
| 300 |
array( 'id' => $snippet_id, 'result' => 'code-error' ), |
| 301 |
code_snippets()->get_menu_url( 'edit' ) |
| 302 |
) ); |
| 303 |
exit; |
| 304 |
} |
| 305 |
|
| 306 |
/* Set the result depending on if the snippet was just added */ |
| 307 |
$result = isset( $_POST['snippet_id'] ) ? 'updated' : 'added'; |
| 308 |
|
| 309 |
/* Append a suffix if the snippet was activated or deactivated */ |
| 310 |
if ( isset( $_POST['save_snippet_activate'] ) ) { |
| 311 |
$result .= '-and-activated'; |
| 312 |
} elseif ( isset( $_POST['save_snippet_deactivate'] ) ) { |
| 313 |
$result .= '-and-deactivated'; |
| 314 |
} elseif ( isset( $_POST['save_snippet_execute'] ) ) { |
| 315 |
$result .= '-and-executed'; |
| 316 |
} |
| 317 |
|
| 318 |
/* Redirect to edit snippet page */ |
| 319 |
$redirect_uri = add_query_arg( |
| 320 |
array( 'id' => $snippet_id, 'result' => $result ), |
| 321 |
code_snippets()->get_menu_url( 'edit' ) |
| 322 |
); |
| 323 |
|
| 324 |
wp_redirect( esc_url_raw( $redirect_uri ) ); |
| 325 |
exit; |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* Add a description editor to the single snippet page |
| 330 |
* |
| 331 |
* @param Code_Snippet $snippet The snippet being used for this page |
| 332 |
*/ |
| 333 |
function render_description_editor( Code_Snippet $snippet ) { |
| 334 |
$settings = code_snippets_get_settings(); |
| 335 |
$settings = $settings['description_editor']; |
| 336 |
$heading = __( 'Description', 'code-snippets' ); |
| 337 |
|
| 338 |
/* Hack to remove space between heading and editor tabs */ |
| 339 |
if ( ! $settings['media_buttons'] && 'false' !== get_user_option( 'rich_editing' ) ) { |
| 340 |
$heading = "<div>$heading</div>"; |
| 341 |
} |
| 342 |
|
| 343 |
echo '<h2><label for="snippet_description">', $heading, '</label></h2>'; |
| 344 |
|
| 345 |
remove_editor_styles(); // stop custom theme styling interfering with the editor |
| 346 |
|
| 347 |
wp_editor( |
| 348 |
$snippet->desc, |
| 349 |
'description', |
| 350 |
apply_filters( 'code_snippets/admin/description_editor_settings', array( |
| 351 |
'textarea_name' => 'snippet_description', |
| 352 |
'textarea_rows' => $settings['rows'], |
| 353 |
'teeny' => ! $settings['use_full_mce'], |
| 354 |
'media_buttons' => $settings['media_buttons'], |
| 355 |
) ) |
| 356 |
); |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* Render the interface for editing snippet tags |
| 361 |
* |
| 362 |
* @param Code_Snippet $snippet the snippet currently being edited |
| 363 |
*/ |
| 364 |
function render_tags_editor( Code_Snippet $snippet ) { |
| 365 |
|
| 366 |
?> |
| 367 |
<h2 style="margin: 25px 0 10px;"> |
| 368 |
<label for="snippet_tags" style="cursor: auto;"> |
| 369 |
<?php esc_html_e( 'Tags', 'code-snippets' ); ?> |
| 370 |
</label> |
| 371 |
</h2> |
| 372 |
|
| 373 |
<input type="text" id="snippet_tags" name="snippet_tags" style="width: 100%;" |
| 374 |
placeholder="<?php esc_html_e( 'Enter a list of tags; separated by commas', 'code-snippets' ); ?>" |
| 375 |
value="<?php echo esc_attr( $snippet->tags_list ); ?>" /> |
| 376 |
<?php |
| 377 |
} |
| 378 |
|
| 379 |
/** |
| 380 |
* Render the snippet priority setting |
| 381 |
* |
| 382 |
* @param Code_Snippet $snippet the snippet currently being edited |
| 383 |
*/ |
| 384 |
public function render_priority_setting( Code_Snippet $snippet ) { |
| 385 |
?> |
| 386 |
<p class="snippet-priority" |
| 387 |
title="<?php esc_attr_e( 'Snippets with a lower priority number will run before those with a higher number.', 'code-snippets' ); ?>"> |
| 388 |
<label for="snippet_priority"><?php esc_html_e( 'Priority', 'code-snippets' ); ?></label> |
| 389 |
|
| 390 |
<input name="snippet_priority" type="number" id="snippet_priority" value="<?php echo intval( $snippet->priority ); ?>"> |
| 391 |
</p> |
| 392 |
<?php |
| 393 |
} |
| 394 |
|
| 395 |
/** |
| 396 |
* Render the snippet scope setting |
| 397 |
* |
| 398 |
* @param Code_Snippet $snippet the snippet currently being edited |
| 399 |
*/ |
| 400 |
function render_scope_setting( Code_Snippet $snippet ) { |
| 401 |
|
| 402 |
$icons = Code_Snippet::get_scope_icons(); |
| 403 |
|
| 404 |
$labels = array( |
| 405 |
'global' => __( 'Run snippet everywhere', 'code-snippets' ), |
| 406 |
'admin' => __( 'Only run in administration area', 'code-snippets' ), |
| 407 |
'front-end' => __( 'Only run on site front-end', 'code-snippets' ), |
| 408 |
'single-use' => __( 'Only run once', 'code-snippets' ), |
| 409 |
); |
| 410 |
|
| 411 |
echo '<h2 class="screen-reader-text">' . esc_html__( 'Scope', 'code-snippets' ) . '</h2><p class="snippet-scope">'; |
| 412 |
|
| 413 |
foreach ( Code_Snippet::get_all_scopes() as $scope ) { |
| 414 |
printf( '<label><input type="radio" name="snippet_scope" value="%s"', $scope ); |
| 415 |
checked( $scope, $snippet->scope ); |
| 416 |
printf( '> <span class="dashicons dashicons-%s"></span> %s</label>', $icons[ $scope ], esc_html( $labels[ $scope ] ) ); |
| 417 |
} |
| 418 |
|
| 419 |
echo '</p>'; |
| 420 |
} |
| 421 |
|
| 422 |
/** |
| 423 |
* Render the setting for shared network snippets |
| 424 |
* |
| 425 |
* @param object $snippet The snippet currently being edited |
| 426 |
*/ |
| 427 |
function render_multisite_sharing_setting( $snippet ) { |
| 428 |
$shared_snippets = get_site_option( 'shared_network_snippets', array() ); |
| 429 |
?> |
| 430 |
|
| 431 |
<div class="snippet-sharing-setting"> |
| 432 |
<h2 class="screen-reader-text"><?php _e( 'Sharing Settings', 'code-snippets' ); ?></h2> |
| 433 |
<label for="snippet_sharing"> |
| 434 |
<input type="checkbox" name="snippet_sharing" |
| 435 |
<?php checked( in_array( $snippet->id, $shared_snippets, true ) ); ?>> |
| 436 |
<?php esc_html_e( 'Allow this snippet to be activated on individual sites on the network', 'code-snippets' ); ?> |
| 437 |
</label> |
| 438 |
</div> |
| 439 |
|
| 440 |
<?php |
| 441 |
} |
| 442 |
|
| 443 |
/** |
| 444 |
* Retrieve the first error in a snippet's code |
| 445 |
* |
| 446 |
* @param $snippet_id |
| 447 |
* |
| 448 |
* @return array|bool |
| 449 |
*/ |
| 450 |
private function get_snippet_error( $snippet_id ) { |
| 451 |
|
| 452 |
if ( ! intval( $snippet_id ) ) { |
| 453 |
return false; |
| 454 |
} |
| 455 |
|
| 456 |
$snippet = get_snippet( intval( $snippet_id ) ); |
| 457 |
|
| 458 |
if ( '' === $snippet->code ) { |
| 459 |
return false; |
| 460 |
} |
| 461 |
|
| 462 |
$validator = new Code_Snippets_Validator( $snippet->code ); |
| 463 |
|
| 464 |
if ( $error = $validator->validate() ) { |
| 465 |
return $error; |
| 466 |
} |
| 467 |
|
| 468 |
ob_start(); |
| 469 |
$result = eval( $snippet->code ); |
| 470 |
ob_end_clean(); |
| 471 |
|
| 472 |
if ( false !== $result ) { |
| 473 |
return false; |
| 474 |
} |
| 475 |
|
| 476 |
$error = error_get_last(); |
| 477 |
|
| 478 |
if ( is_null( $error ) ) { |
| 479 |
return false; |
| 480 |
} |
| 481 |
|
| 482 |
return $error; |
| 483 |
} |
| 484 |
|
| 485 |
/** |
| 486 |
* Print the status and error messages |
| 487 |
*/ |
| 488 |
protected function print_messages() { |
| 489 |
|
| 490 |
if ( ! isset( $_REQUEST['result'] ) ) { |
| 491 |
return; |
| 492 |
} |
| 493 |
|
| 494 |
$result = $_REQUEST['result']; |
| 495 |
|
| 496 |
if ( 'code-error' === $result ) { |
| 497 |
|
| 498 |
if ( isset( $_REQUEST['id'] ) && $error = $this->get_snippet_error( $_REQUEST['id'] ) ) { |
| 499 |
|
| 500 |
printf( |
| 501 |
'<div id="message" class="error fade"><p>%s</p><p><strong>%s</strong></p></div>', |
| 502 |
/* translators: %d: line of file where error originated */ |
| 503 |
sprintf( __( 'The snippet has been deactivated due to an error on line %d:', 'code-snippets' ), $error['line'] ), |
| 504 |
$error['message'] |
| 505 |
); |
| 506 |
|
| 507 |
} else { |
| 508 |
echo '<div id="message" class="error fade"><p>', __( 'The snippet has been deactivated due to an error in the code.', 'code-snippets' ), '</p></div>'; |
| 509 |
} |
| 510 |
|
| 511 |
return; |
| 512 |
} |
| 513 |
|
| 514 |
if ( 'save-error' === $result ) { |
| 515 |
echo '<div id="message" class="error fade"><p>', __( 'An error occurred when saving the snippet.', 'code-snippets' ), '</p></div>'; |
| 516 |
|
| 517 |
return; |
| 518 |
} |
| 519 |
|
| 520 |
$messages = array( |
| 521 |
'added' => __( 'Snippet <strong>added</strong>.', 'code-snippets' ), |
| 522 |
'updated' => __( 'Snippet <strong>updated</strong>.', 'code-snippets' ), |
| 523 |
'added-and-activated' => __( 'Snippet <strong>added</strong> and <strong>activated</strong>.', 'code-snippets' ), |
| 524 |
'updated-and-executed' => __( 'Snippet <strong>added</strong> and <strong>executed</strong>.', 'code-snippets' ), |
| 525 |
'updated-and-activated' => __( 'Snippet <strong>updated</strong> and <strong>activated</strong>.', 'code-snippets' ), |
| 526 |
'updated-and-deactivated' => __( 'Snippet <strong>updated</strong> and <strong>deactivated</strong>.', 'code-snippets' ), |
| 527 |
); |
| 528 |
|
| 529 |
if ( isset( $messages[ $result ] ) ) { |
| 530 |
echo '<div id="message" class="updated fade"><p>', $messages[ $result ], '</p></div>'; |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
/** |
| 535 |
* Enqueue assets for the edit menu |
| 536 |
*/ |
| 537 |
public function enqueue_assets() { |
| 538 |
$plugin = code_snippets(); |
| 539 |
$rtl = is_rtl() ? '-rtl' : ''; |
| 540 |
|
| 541 |
code_snippets_enqueue_editor(); |
| 542 |
|
| 543 |
wp_enqueue_style( |
| 544 |
'code-snippets-edit', |
| 545 |
plugins_url( "css/min/edit{$rtl}.css", $plugin->file ), |
| 546 |
array(), $plugin->version |
| 547 |
); |
| 548 |
|
| 549 |
wp_enqueue_script( |
| 550 |
'code-snippets-edit-menu', |
| 551 |
plugins_url( 'js/min/edit.js', $plugin->file ), |
| 552 |
array(), $plugin->version, true |
| 553 |
); |
| 554 |
|
| 555 |
$atts = code_snippets_get_editor_atts( array(), true ); |
| 556 |
$inline_script = 'var code_snippets_editor_atts = ' . $atts . ';'; |
| 557 |
|
| 558 |
wp_add_inline_script( 'code-snippets-edit-menu', $inline_script, 'before' ); |
| 559 |
|
| 560 |
if ( code_snippets_get_setting( 'general', 'enable_tags' ) ) { |
| 561 |
|
| 562 |
wp_enqueue_script( |
| 563 |
'code-snippets-edit-menu-tags', |
| 564 |
plugins_url( 'js/min/edit-tags.js', $plugin->file ), |
| 565 |
array( |
| 566 |
'jquery', 'jquery-ui-core', |
| 567 |
'jquery-ui-widget', 'jquery-ui-position', 'jquery-ui-autocomplete', |
| 568 |
'jquery-effects-blind', 'jquery-effects-highlight', |
| 569 |
), |
| 570 |
$plugin->version, true |
| 571 |
); |
| 572 |
|
| 573 |
$snippet_tags = wp_json_encode( get_all_snippet_tags() ); |
| 574 |
$inline_script = 'var code_snippets_all_tags = ' . $snippet_tags . ';'; |
| 575 |
|
| 576 |
wp_add_inline_script( 'code-snippets-edit-menu-tags', $inline_script, 'before' ); |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
/** |
| 581 |
* Remove the old CodeMirror version used by the Debug Bar Console plugin |
| 582 |
* that is messing up the snippet editor |
| 583 |
*/ |
| 584 |
function remove_debug_bar_codemirror() { |
| 585 |
|
| 586 |
/* Try to discern if we are on the single snippet page as best as we can at this early time */ |
| 587 |
if ( ! is_admin() || 'admin.php' !== $GLOBALS['pagenow'] ) { |
| 588 |
return; |
| 589 |
} |
| 590 |
|
| 591 |
if ( ! isset( $_GET['page'] ) || code_snippets()->get_menu_slug( 'edit' ) !== $_GET['page'] && code_snippets()->get_menu_slug( 'settings' ) !== $_GET['page'] ) { |
| 592 |
return; |
| 593 |
} |
| 594 |
|
| 595 |
remove_action( 'debug_bar_enqueue_scripts', 'debug_bar_console_scripts' ); |
| 596 |
} |
| 597 |
|
| 598 |
/** |
| 599 |
* Retrieve a list of submit actions for a given snippet |
| 600 |
* |
| 601 |
* @param Code_Snippet $snippet |
| 602 |
* @param bool $extra_actions |
| 603 |
* |
| 604 |
* @return array |
| 605 |
*/ |
| 606 |
public function get_actions_list( $snippet, $extra_actions = true ) { |
| 607 |
$actions = array( |
| 608 |
'save_snippet' => __( 'Save Changes', 'code-snippets' ), |
| 609 |
); |
| 610 |
|
| 611 |
if ( 'single-use' === $snippet->scope ) { |
| 612 |
$actions['save_snippet_execute'] = __( 'Save Changes and Execute Once', 'code-snippets' ); |
| 613 |
|
| 614 |
} elseif ( ! $snippet->shared_network || ! is_network_admin() ) { |
| 615 |
|
| 616 |
if ( $snippet->active ) { |
| 617 |
$actions['save_snippet_deactivate'] = __( 'Save Changes and Deactivate', 'code-snippets' ); |
| 618 |
} else { |
| 619 |
$actions['save_snippet_activate'] = __( 'Save Changes and Activate', 'code-snippets' ); |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
// Make the 'Save and Activate' button the default if the setting is enabled |
| 624 |
if ( ! $snippet->active && 'single-use' !== $snippet->scope && |
| 625 |
code_snippets_get_setting( 'general', 'activate_by_default' ) ) { |
| 626 |
$actions = array_reverse( $actions ); |
| 627 |
} |
| 628 |
|
| 629 |
if ( $extra_actions && 0 !== $snippet->id ) { |
| 630 |
|
| 631 |
if ( apply_filters( 'code_snippets/enable_downloads', true ) ) { |
| 632 |
$actions['download_snippet'] = __( 'Download', 'code-snippets' ); |
| 633 |
} |
| 634 |
|
| 635 |
$actions['export_snippet'] = __( 'Export', 'code-snippets' ); |
| 636 |
$actions['delete_snippet'] = __( 'Delete', 'code-snippets' ); |
| 637 |
} |
| 638 |
|
| 639 |
return $actions; |
| 640 |
} |
| 641 |
|
| 642 |
/** |
| 643 |
* Render the submit buttons for a code snippet |
| 644 |
* |
| 645 |
* @param Code_Snippet $snippet |
| 646 |
* @param string $size |
| 647 |
* @param bool $extra_actions |
| 648 |
*/ |
| 649 |
public function render_submit_buttons( $snippet, $size = '', $extra_actions = true ) { |
| 650 |
|
| 651 |
$actions = $this->get_actions_list( $snippet, $extra_actions ); |
| 652 |
$type = 'primary'; |
| 653 |
$size = $size ? ' ' . $size : ''; |
| 654 |
|
| 655 |
foreach ( $actions as $action => $label ) { |
| 656 |
$other = null; |
| 657 |
|
| 658 |
if ( 'delete_snippet' === $action ) { |
| 659 |
|
| 660 |
$other = sprintf( 'onclick="%s"', esc_js( |
| 661 |
sprintf( |
| 662 |
'return confirm("%s");', |
| 663 |
__( 'You are about to permanently delete this snippet.', 'code-snippets' ) . "\n" . |
| 664 |
__( "'Cancel' to stop, 'OK' to delete.", 'code-snippets' ) |
| 665 |
) |
| 666 |
) ); |
| 667 |
} |
| 668 |
|
| 669 |
submit_button( $label, $type . $size, $action, false, $other ); |
| 670 |
|
| 671 |
if ( 'primary' === $type ) { |
| 672 |
$type = 'secondary'; |
| 673 |
} |
| 674 |
} |
| 675 |
} |
| 676 |
} |
| 677 |
|