PluginProbe
Code Snippets / 2.12.1
Code Snippets v2.12.1
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 / views / edit.php

edit.php in Code Snippets 2.12.1, at php/views/edit.php

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