| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* HTML code for the Add New/Edit Snippet page |
| 5 |
* |
| 6 |
* @package Code_Snippets |
| 7 |
* @subpackage Views |
| 8 |
*/ |
| 9 |
|
| 10 |
/* Bail if accessed directly */ |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
return; |
| 13 |
} |
| 14 |
|
| 15 |
$table = code_snippets()->db->get_table_name(); |
| 16 |
$edit_id = code_snippets()->get_menu_slug( 'edit' ) === $_REQUEST['page'] ? absint( $_REQUEST['id'] ) : 0; |
| 17 |
$snippet = get_snippet( $edit_id ); |
| 18 |
|
| 19 |
$classes = array(); |
| 20 |
|
| 21 |
if ( $edit_id ) { |
| 22 |
$classes[] = ( $snippet->active ? '' : 'in' ) . 'active-snippet'; |
| 23 |
} else { |
| 24 |
$classes[] = 'new-snippet'; |
| 25 |
} |
| 26 |
|
| 27 |
?> |
| 28 |
<div class="wrap"> |
| 29 |
<h1><?php |
| 30 |
if ( $edit_id ) { |
| 31 |
esc_html_e( 'Edit Snippet', 'code-snippets' ); |
| 32 |
printf( ' <a href="%1$s" class="page-title-action add-new-h2">%2$s</a>', |
| 33 |
code_snippets()->get_menu_url( 'add' ), |
| 34 |
esc_html_x( 'Add New', 'snippet', 'code-snippets' ) |
| 35 |
); |
| 36 |
} else { |
| 37 |
esc_html_e( 'Add New Snippet', 'code-snippets' ); |
| 38 |
} |
| 39 |
?></h1> |
| 40 |
|
| 41 |
<form method="post" id="snippet-form" action="" style="margin-top: 10px;" class="<?php echo implode( ' ', $classes ); ?>"> |
| 42 |
<?php |
| 43 |
/* Output the hidden fields */ |
| 44 |
|
| 45 |
if ( 0 !== $snippet->id ) { |
| 46 |
printf( '<input type="hidden" name="snippet_id" value="%d" />', $snippet->id ); |
| 47 |
} |
| 48 |
|
| 49 |
printf( '<input type="hidden" name="snippet_active" value="%d" />', $snippet->active ); |
| 50 |
|
| 51 |
?> |
| 52 |
<div id="titlediv"> |
| 53 |
<div id="titlewrap"> |
| 54 |
<label for="title" style="display: none;"><?php _e( 'Name', 'code-snippets' ); ?></label> |
| 55 |
<input id="title" type="text" autocomplete="off" name="snippet_name" value="<?php echo esc_attr( $snippet->name ); ?>" placeholder="<?php _e( 'Enter title here', 'code-snippets' ); ?>" /> |
| 56 |
</div> |
| 57 |
</div> |
| 58 |
|
| 59 |
<h2><label for="snippet_code"> |
| 60 |
<?php _e( 'Code', 'code-snippets' ); ?> |
| 61 |
</label></h2> |
| 62 |
|
| 63 |
<textarea id="snippet_code" name="snippet_code" rows="200" spellcheck="false" style="font-family: monospace; width: 100%;"><?php |
| 64 |
echo esc_textarea( $snippet->code ); |
| 65 |
?></textarea> |
| 66 |
|
| 67 |
<?php |
| 68 |
/* Allow plugins to add fields and content to this page */ |
| 69 |
do_action( 'code_snippets/admin/single', $snippet ); |
| 70 |
|
| 71 |
/* Add a nonce for security */ |
| 72 |
wp_nonce_field( 'save_snippet' ); |
| 73 |
|
| 74 |
?> |
| 75 |
|
| 76 |
<p class="submit"> |
| 77 |
<?php |
| 78 |
|
| 79 |
/* Make the 'Save and Activate' button the default if the setting is enabled */ |
| 80 |
|
| 81 |
if ( 'single-use' === $snippet->scope ) { |
| 82 |
|
| 83 |
submit_button( null, 'primary', 'save_snippet', false ); |
| 84 |
|
| 85 |
submit_button( __( 'Save Changes and Execute Once', 'code-snippets' ), 'secondary', 'save_snippet_execute', false ); |
| 86 |
|
| 87 |
} elseif ( $snippet->shared_network && is_network_admin() ) { |
| 88 |
|
| 89 |
submit_button( null, 'primary', 'save_snippet', false ); |
| 90 |
|
| 91 |
} elseif ( ! $snippet->active && code_snippets_get_setting( 'general', 'activate_by_default' ) ) { |
| 92 |
|
| 93 |
submit_button( |
| 94 |
__( 'Save Changes and Activate', 'code-snippets' ), |
| 95 |
'primary', 'save_snippet_activate', false |
| 96 |
); |
| 97 |
|
| 98 |
submit_button( null, 'secondary', 'save_snippet', false ); |
| 99 |
|
| 100 |
} else { |
| 101 |
|
| 102 |
/* Save Snippet button */ |
| 103 |
submit_button( null, 'primary', 'save_snippet', false ); |
| 104 |
|
| 105 |
/* Save Snippet and Activate/Deactivate button */ |
| 106 |
if ( ! $snippet->active ) { |
| 107 |
submit_button( |
| 108 |
__( 'Save Changes and Activate', 'code-snippets' ), |
| 109 |
'secondary', 'save_snippet_activate', false |
| 110 |
); |
| 111 |
|
| 112 |
} else { |
| 113 |
submit_button( |
| 114 |
__( 'Save Changes and Deactivate', 'code-snippets' ), |
| 115 |
'secondary', 'save_snippet_deactivate', false |
| 116 |
); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
if ( 0 !== $snippet->id ) { |
| 121 |
|
| 122 |
/* Download button */ |
| 123 |
|
| 124 |
if ( apply_filters( 'code_snippets/enable_downloads', true ) ) { |
| 125 |
submit_button( __( 'Download', 'code-snippets' ), 'secondary', 'download_snippet', false ); |
| 126 |
} |
| 127 |
|
| 128 |
/* Export button */ |
| 129 |
|
| 130 |
submit_button( __( 'Export', 'code-snippets' ), 'secondary', 'export_snippet', false ); |
| 131 |
|
| 132 |
/* Delete button */ |
| 133 |
|
| 134 |
$confirm_delete_js = esc_js( |
| 135 |
sprintf( |
| 136 |
'return confirm("%s");', |
| 137 |
__( 'You are about to permanently delete this snippet.', 'code-snippets' ) . "\n" . |
| 138 |
__( "'Cancel' to stop, 'OK' to delete.", 'code-snippets' ) |
| 139 |
) |
| 140 |
); |
| 141 |
|
| 142 |
submit_button( |
| 143 |
__( 'Delete', 'code-snippets' ), |
| 144 |
'secondary', 'delete_snippet', false, |
| 145 |
sprintf( 'onclick="%s"', $confirm_delete_js ) |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
?> |
| 150 |
</p> |
| 151 |
</form> |
| 152 |
</div> |
| 153 |
|
| 154 |
<script> |
| 155 |
'use strict'; |
| 156 |
/* Loads CodeMirror on the snippet editor */ |
| 157 |
var code_snippet_editor = (function() { |
| 158 |
|
| 159 |
var atts = []; |
| 160 |
atts = <?php |
| 161 |
$atts = array( 'mode' => 'text/x-php' ); |
| 162 |
echo code_snippets_get_editor_atts( $atts, true ); |
| 163 |
?>; |
| 164 |
atts['viewportMargin'] = Infinity; |
| 165 |
|
| 166 |
atts['extraKeys'] = { |
| 167 |
'Ctrl-Enter': function (cm) { |
| 168 |
document.getElementById('snippet-form').submit(); |
| 169 |
} |
| 170 |
}; |
| 171 |
|
| 172 |
var editor = CodeMirror.fromTextArea(document.getElementById('snippet_code'), atts); |
| 173 |
|
| 174 |
// set the cursor to the previous position |
| 175 |
var matches = window.location.href.match(/[?&]cursor=(\d+)_(\d+)/); |
| 176 |
if (matches) { |
| 177 |
editor.focus(); |
| 178 |
editor.setCursor({line: matches[1], ch: matches[2]}); |
| 179 |
} |
| 180 |
|
| 181 |
// send the current cursor position to the next page |
| 182 |
document.getElementById('snippet-form').addEventListener('submit', function () { |
| 183 |
var cursor = editor.getCursor(); |
| 184 |
this.insertAdjacentHTML('beforeend', '<input type="hidden" name="snippet_editor_cursor" value="' + cursor.line + '_' + cursor.ch + '">'); |
| 185 |
}); |
| 186 |
|
| 187 |
// submit snippet on ctrl/cmd+s |
| 188 |
document.addEventListener('keydown', function (e) { |
| 189 |
if ((window.navigator.platform.match('Mac') ? e.metaKey : e.ctrlKey) && 's' === e.key) { |
| 190 |
e.preventDefault(); |
| 191 |
document.getElementById('save_snippet').click(); |
| 192 |
} |
| 193 |
}, false); |
| 194 |
|
| 195 |
return editor; |
| 196 |
})(); |
| 197 |
</script> |
| 198 |
|