PluginProbe ʕ •ᴥ•ʔ
Code Manager / 1.0.3
Code Manager v1.0.3
1.0.47 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.3 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 1.0.39 1.0.4 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9
code-manager / Code_Manager / Code_Manager_Form.php
code-manager / Code_Manager Last commit date
Code_Manager.php 5 years ago Code_Manager_Export.php 5 years ago Code_Manager_Form.php 5 years ago Code_Manager_Import.php 5 years ago Code_Manager_Import_File.php 5 years ago Code_Manager_List.php 5 years ago Code_Manager_List_View.php 5 years ago Code_Manager_Model.php 5 years ago Code_Manager_Preview.php 5 years ago Code_Manager_Settings.php 5 years ago Code_Manager_Tabs.php 5 years ago Message_Box.php 5 years ago WP_List_Table.php 5 years ago
Code_Manager_Form.php
411 lines
1 <?php
2
3 namespace Code_Manager {
4
5 /**
6 * Class Code_Manager_Form
7 *
8 * Implements data entry form for Code Manager.
9 *
10 * @author Peter Schulz
11 * @since 1.0.0
12 */
13 class Code_Manager_Form {
14
15 /**
16 * Actual code manager record
17 *
18 * @var null|array
19 */
20 protected $row = null;
21
22 /**
23 * Allowed values: view (read-only mode) and edit (update mode)
24 *
25 * @var string
26 */
27 protected $action = 'edit';
28
29 /**
30 * Allowed values: null (no DML action needed) and save (perform insert or update)
31 *
32 * @var null|string
33 */
34 protected $action2 = null;
35
36 /**
37 * Code ID. Must be entered to view or edit. Allows null when action = new (insert).
38 *
39 * @var int|null
40 */
41 protected $code_id = null;
42
43 /**
44 * Actual preview mode for current Code ID
45 *
46 * @var bool
47 */
48 protected $code_manager_preview = false;
49
50 /**
51 * Title added to preview link
52 *
53 * @var string
54 */
55 protected $code_manager_preview_title;
56 protected $code_manager_preview_title_off;
57 protected $code_manager_preview_title_on;
58
59 /**
60 * WP Nonce used for DML actions.
61 *
62 * @var string
63 */
64 protected $wpnone_activate_code_preview;
65
66 /**
67 * Code_Manager_Form constructor.
68 *
69 * Initializes data entry form and performs DML actions as requested by arguments.
70 *
71 * @since 1.0.0
72 */
73 public function __construct() {
74 $this->action =
75 isset( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ) : 'edit'; // input var okay.
76
77 $this->action2 =
78 isset( $_REQUEST['action2'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['action2'] ) ) : null; // input var okay.
79
80 $this->code_id =
81 isset( $_REQUEST['code_id'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['code_id'] ) ) : null; // input var okay.
82
83 switch( $this->action ) {
84 case 'edit':
85 if ( null === $this->code_id ) {
86 wp_die( __( 'ERROR: Invalid arguments', 'code-manager' ) );
87 }
88 if ( 'save' === $this->action2 ) {
89 $this->check_authorization(); // Dies if not authorized
90 if (
91 isset( $_REQUEST['code_id'] ) &&
92 isset( $_REQUEST['code_name'] ) &&
93 isset( $_REQUEST['code_type'] ) &&
94 isset( $_REQUEST['code'] ) &&
95 isset( $_REQUEST['code_author'] ) &&
96 isset( $_REQUEST['code_description'] )
97 ) {
98 // All data available, start update process
99 $code_id = sanitize_text_field( wp_unslash( $_REQUEST['code_id'] ) ); // input var okay.
100 $code_name = sanitize_text_field( wp_unslash( $_REQUEST['code_name'] ) ); // input var okay.
101 $code_type = sanitize_text_field( wp_unslash( $_REQUEST['code_type'] ) ); // input var okay.
102 $code = wp_unslash( $_REQUEST['code'] ); // input var okay.
103 $code_author = sanitize_text_field( wp_unslash( $_REQUEST['code_author'] ) ); // input var okay.
104 $code_description = wp_unslash( $_REQUEST['code_description'] ); // input var okay.
105
106 $code_manager_model_class = CODE_MANAGER_MODEL_CLASS;
107 $code_manager_model = new $code_manager_model_class();
108 $numrows = $code_manager_model::dml_update( $code_id, $code_name, $code_type, $code, $code_author, $code_description );
109 if ( 0 === $numrows ) {
110 $msg = new Message_Box(
111 [
112 'message_text' => __( 'Nothing to save', 'code-manager' ),
113 ]
114 );
115 $msg->box();
116 } elseif ( 1 === $numrows ) {
117 $msg = new Message_Box(
118 [
119 'message_text' => __( 'Succesfully saved changes to database', 'code-manager' ),
120 ]
121 );
122 $msg->box();
123 }
124 } else {
125 // No update possible, missing data
126 $msg = new Message_Box(
127 [
128 'message_text' => __( 'Update failed', 'code-manager' ),
129 'message_type' => 'error',
130 'message_is_dismissible' => false,
131 ]
132 );
133 $msg->box();
134 }
135 }
136 // Requery
137 $code_manager_model_class = CODE_MANAGER_MODEL_CLASS;
138 $code_manager_model = new $code_manager_model_class();
139 $this->row = $code_manager_model::dml_query( $this->code_id );
140 break;
141 case 'new':
142 if ( 'save' === $this->action2 ) {
143 $this->check_authorization(); // Dies if not authorized
144 if (
145 isset( $_REQUEST['code_name'] ) &&
146 isset( $_REQUEST['code_type'] ) &&
147 isset( $_REQUEST['code'] ) &&
148 isset( $_REQUEST['code_author'] ) &&
149 isset( $_REQUEST['code_description'] )
150 ) {
151 // All data available, start insert process
152 $code_name = sanitize_text_field( wp_unslash( $_REQUEST['code_name'] ) ); // input var okay.
153 $code_type = sanitize_text_field( wp_unslash( $_REQUEST['code_type'] ) ); // input var okay.
154 $code = wp_unslash( $_REQUEST['code'] ); // input var okay.
155 $code_author = sanitize_text_field( wp_unslash( $_REQUEST['code_author'] ) ); // input var okay.
156 $code_description = wp_unslash( $_REQUEST['code_description'] ); // input var okay.
157
158 $code_manager_model_class = CODE_MANAGER_MODEL_CLASS;
159 $code_manager_model = new $code_manager_model_class();
160 $code_id = $code_manager_model::dml_insert( $code_name, $code_type, $code, $code_author, $code_description );
161 if ( -1 === $code_id ) {
162 $msg = new Message_Box(
163 [
164 'message_text' => __( 'Insert failed', 'code-manager' ),
165 'message_type' => 'error',
166 'message_is_dismissible' => false,
167 ]
168 );
169 $msg->box();
170 } else {
171 $msg = new Message_Box(
172 [
173 'message_text' => __( 'Succesfully saved changes to database', 'code-manager' ),
174 ]
175 );
176 $msg->box();
177
178 $this->code_id = $code_id;
179 $code_manager_model_class = CODE_MANAGER_MODEL_CLASS;
180 $code_manager_model = new $code_manager_model_class();
181 $this->row = $code_manager_model::dml_query( $this->code_id );
182 $this->action = 'edit';
183 }
184 } else {
185 // No insert possible, missing data
186 $msg = new Message_Box(
187 [
188 'message_text' => __( 'Insert failed', 'code-manager' ),
189 'message_type' => 'error',
190 'message_is_dismissible' => false,
191 ]
192 );
193 $msg->box();
194 }
195 }
196 }
197
198 $this->code_manager_preview_title_off = __( 'Activate preview mode for this code', 'code-manager');
199 $this->code_manager_preview_title_on = __( 'Deactivate preview mode for this code', 'code-manager');
200
201 $this->code_manager_preview = false; // TODO Check if enabled for this code
202 if ( ! $this->code_manager_preview ) {
203 $this->code_manager_preview_title = $this->code_manager_preview_title_off;
204 } else {
205 $this->code_manager_preview_title = $this->code_manager_preview_title_on;
206 }
207
208 $this->wpnone_activate_code_preview = wp_create_nonce( "code-manager-activate-preview" );
209 }
210
211 /**
212 * Changes are only allow with proper authorization
213 *
214 * @since 1.0.0
215 */
216 private function check_authorization() {
217 $wp_nonce = isset( $_REQUEST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ) : ''; // input var okay.
218 if ( ! wp_verify_nonce( $wp_nonce, 'code_manager_editor' ) ) {
219 wp_die( __( 'ERROR: Not authorized', 'code-manager' ) );
220 }
221 }
222
223 /**
224 * Build data entry form. Generates HTML only. JS actions are added from JS script file.
225 *
226 * @since 1.0.0
227 */
228 public function show() {
229 if ( null !== $this->row ) {
230 $code_name = $this->row[0]['code_name'];
231 $code_type = $this->row[0]['code_type'];
232 $code = $this->row[0]['code'];
233 $code_author = $this->row[0]['code_author'];
234 $code_description = $this->row[0]['code_description'];
235 } else {
236 $code_name = '';
237 $code_type = 'php shortcode';
238 $code = "<?php\n\n?>";
239 $code_author = '';
240 $code_description = '';
241 }
242 ?>
243 <div class="wrap">
244 <h1 class="wp-heading-inline">
245 <span>
246 <span class="cm_page_title">
247 <?php echo CODE_MANAGER_H1_TITLE; ?>
248 </span>
249 <a href="?page=<?php echo CODE_MANAGER_MENU_SLUG; ?>"
250 title="Back to list">
251 <span class="material-icons cm_menu_title">menu</span></a>
252 <a href="<?php echo CODE_MANAGER_HELP_URL; ?>" target="_blank"
253 title="Plugin help - opens in a new tab or window">
254 <span class="material-icons cm_menu_title">help_outline</span></a>
255 </span>
256 </h1>
257 <p></p>
258 <div>
259 <form method="post" enctype="multipart/form-data"
260 action="?page=<?php echo CODE_MANAGER_MENU_SLUG; ?>">
261 <fieldset class="cm_fieldset">
262 <table class="cm_simple_table" cellspacing="0" cellpadding="0">
263 <tbody>
264 <tr>
265 <td class="label">
266 <label for="code_id" title="Code ID must be entered">
267 * Code ID
268 </label>
269 </td>
270 <td class="data">
271 <input name="code_id" id="code_id" type="text"
272 value="<?php echo esc_attr( $this->code_id ); ?>" readonly="">
273 </td>
274 <td class="icon">
275 <span class="cm_data_type">123</span>
276 </td>
277 </tr>
278 <tr>
279 <td class="label">
280 <label for="code_name" title="Name must be entered">
281 * Name
282 </label>
283 </td>
284 <td class="data">
285 <input name="code_name" id="code_name" type="text" maxlength="100"
286 value="<?php echo esc_attr( $code_name ); ?>">
287 </td>
288 <td class="icon">
289 <span class="cm_data_type">abc</span></td>
290 </tr>
291 <tr>
292 <td class="label">
293 <label for="code_type" title="Type must be entered">
294 Type
295 </label>
296 </td>
297 <td class="data">
298 <select name="code_type" id="code_type">
299 <?php
300 $code_manager_tab_class = CODE_MANAGER_TAB_CLASS;
301 $code_manager_tab = new $code_manager_tab_class();
302 $code_types = $code_manager_tab->get_code_types();
303 foreach ( $code_types as $code_type_group => $value ) {
304 echo "<optgroup label='{$code_type_group}'>";
305 foreach ( $value as $value_code_type => $value_code_label ) {
306 echo "<option value='{$value_code_type}'>{$value_code_label}</option>";
307 }
308 echo '</optgroup>';
309 }
310 ?>
311 </select>
312 <script type="text/javascript">
313 jQuery('#code_type').val('<?php echo esc_attr( $code_type ); ?>');
314 </script>
315 </td>
316 <td class="icon">
317 </td>
318 </tr>
319 <tr>
320 <td class="label" style="vertical-align:top;padding-top:7px;">
321 <label for="code" title="Code must be entered">
322 Code
323 </label>
324 </td>
325 <td class="data" style="display: grid; width: 100%;">
326 <textarea name="code" id="code" style="vertical-align: top; display: none;"
327 maxlength="65535"><?php echo str_replace( "&", "&amp;", $code ); ?></textarea>
328 </td>
329 <td class="icon" style="vertical-align:top;padding-top:7px;">
330 <a id="code_manager_preview" title="<?php echo $this->code_manager_preview_title; ?>"
331 class="dashicons <?php echo false!==$this->code_manager_preview ? 'dashicons-hidden' : 'dashicons-visibility'; ?>"
332 ></a>
333 </td>
334 </tr>
335 <tr>
336 <td class="label">
337 <label for="code_author" title="Optional">
338 Author
339 </label>
340 </td>
341 <td class="data">
342 <input name="code_author" id="code_author" type="text" maxlength="100"
343 value="<?php echo esc_attr( $code_author ); ?>">
344 </td>
345 <td class="icon">
346 <span class="cm_data_type">abc</span></td>
347 </tr>
348 <tr>
349 <td class="label" style="vertical-align:top;padding-top:7px;">
350 <label for="code_description" title="Optional">
351 Description
352 </label>
353 </td>
354 <td class="data">
355 <textarea name="code_description" id="code_description" maxlength="65536"
356 ><?php echo esc_attr( $code_description ); ?></textarea>
357 </td>
358 <td></td>
359 </tr>
360 </tbody>
361 </table>
362 </fieldset>
363 <p></p>
364 <div>
365 <input name="action" type="hidden" value="<?php echo $this->action; ?>">
366 <input name="action2" type="hidden" value="save">
367 <?php wp_nonce_field( 'code_manager_editor', '_wpnonce', false ); ?>
368 <input type="submit" id="submit_button" value="Save changes to database"
369 class="button button-primary" name="submit_button" onclick="return submit_form();">
370 <input type="button" onclick="javascript:location.href='?page=<?php echo CODE_MANAGER_MENU_SLUG; ?>'"
371 class="button button-secondary" value="Back to list">
372 </div>
373 </form>
374 </div>
375 </div>
376 <script type="text/javascript">
377 var wpnone_activate_code_preview = '<?php echo $this->wpnone_activate_code_preview; ?>';
378
379 function submit_form() {
380 if (jQuery('#code_name').val()==='') {
381 alert('Name must be entered');
382 return false;
383 }
384 user_has_edited = false;
385 return true;
386 }
387
388 jQuery(function() {
389 jQuery('#code_manager_preview').on('click', function() {
390 if (jQuery('#code_manager_preview').hasClass('dashicons-visibility')) {
391 // Activate preview
392 activate_code();
393
394 jQuery('#code_manager_preview').removeClass('dashicons-visibility').addClass('dashicons-hidden');
395 jQuery('#code_manager_preview').attr('title', '<?php echo $this->code_manager_preview_title_on; ?>');
396 } else {
397 // Deactivate preview
398 deactivate_code();
399
400 jQuery('#code_manager_preview').addClass('dashicons-visibility').removeClass('dashicons-hidden');
401 jQuery('#code_manager_preview').attr('title', '<?php echo $this->code_manager_preview_title_off; ?>');
402 }
403 });
404 });
405 </script>
406 <?php
407 }
408
409 }
410
411 }