PluginProbe ʕ •ᴥ•ʔ
Code Manager / 1.0.0
Code Manager v1.0.0
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
398 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 ?>
230 <div class="wrap">
231 <h1 class="wp-heading-inline">
232 <span>
233 <a href="?page=<?php echo CODE_MANAGER_MENU_SLUG; ?>"
234 title="Back to list">
235 <span class="material-icons cm_menu_title">list</span></a>
236 <span class="cm_page_title">
237 <?php echo CODE_MANAGER_H1_TITLE; ?>
238 </span>
239 <a href="<?php echo CODE_MANAGER_HELP_URL; ?>" target="_blank"
240 title="Plugin help - opens in a new tab or window">
241 <span class="material-icons cm_menu_title">help_outline</span></a>
242 </span>
243 </h1>
244 <p></p>
245 <div>
246 <form method="post" enctype="multipart/form-data"
247 action="?page=<?php echo CODE_MANAGER_MENU_SLUG; ?>">
248 <fieldset class="cm_fieldset">
249 <table class="cm_simple_table" cellspacing="0" cellpadding="0">
250 <tbody>
251 <tr>
252 <td class="label">
253 <label for="code_id" title="Code ID must be entered">
254 * Code ID
255 </label>
256 </td>
257 <td class="data">
258 <input name="code_id" id="code_id" type="text"
259 value="<?php echo esc_attr( $this->code_id ); ?>" readonly="">
260 </td>
261 <td class="icon">
262 <span class="cm_data_type">123</span>
263 </td>
264 </tr>
265 <tr>
266 <td class="label">
267 <label for="code_name" title="Name must be entered">
268 * Name
269 </label>
270 </td>
271 <td class="data">
272 <input name="code_name" id="code_name" type="text" maxlength="100"
273 value="<?php echo esc_attr( $this->row[0]['code_name'] ); ?>">
274 </td>
275 <td class="icon">
276 <span class="cm_data_type">abc</span></td>
277 </tr>
278 <tr>
279 <td class="label">
280 <label for="code_type" title="Type must be entered">
281 Type
282 </label>
283 </td>
284 <td class="data">
285 <select name="code_type" id="code_type">
286 <?php
287 $code_manager_tab_class = CODE_MANAGER_TAB_CLASS;
288 $code_manager_tab = new $code_manager_tab_class();
289 $code_types = $code_manager_tab->get_code_types();
290 foreach ( $code_types as $code_type_group => $value ) {
291 echo "<optgroup label='{$code_type_group}'>";
292 foreach ( $value as $code_type => $code_label ) {
293 echo "<option value='{$code_type}'>{$code_label}</option>";
294 }
295 echo '</optgroup>';
296 }
297 ?>
298 </select>
299 <script type="text/javascript">
300 jQuery('#code_type').val('<?php echo 'new' === $this->action ? 'php shortcode' : $this->row[0]['code_type']; ?>');
301 </script>
302 </td>
303 <td class="icon">
304 </td>
305 </tr>
306 <tr>
307 <td class="label" style="vertical-align:top;padding-top:7px;">
308 <label for="code" title="Code must be entered">
309 Code
310 </label>
311 </td>
312 <td class="data" style="display: grid; width: 100%;">
313 <textarea name="code" id="code" style="vertical-align: top; display: none;"
314 maxlength="65535"><?php echo 'new' !== $this->action ? $this->row[0]['code'] : "<?php\n\n?>"; ?></textarea>
315 </td>
316 <td class="icon" style="vertical-align:top;padding-top:7px;">
317 <a id="code_manager_preview" title="<?php echo $this->code_manager_preview_title; ?>"
318 class="dashicons <?php echo false!==$this->code_manager_preview ? 'dashicons-hidden' : 'dashicons-visibility'; ?>"
319 ></a>
320 </td>
321 </tr>
322 <tr>
323 <td class="label">
324 <label for="code_author" title="Optional">
325 Author
326 </label>
327 </td>
328 <td class="data">
329 <input name="code_author" id="code_author" type="text" maxlength="100"
330 value="<?php echo esc_attr( $this->row[0]['code_author'] ); ?>">
331 </td>
332 <td class="icon">
333 <span class="cm_data_type">abc</span></td>
334 </tr>
335 <tr>
336 <td class="label" style="vertical-align:top;padding-top:7px;">
337 <label for="code_description" title="Optional">
338 Description
339 </label>
340 </td>
341 <td class="data">
342 <textarea name="code_description" id="code_description" maxlength="65536"
343 ><?php echo esc_attr( $this->row[0]['code_description'] ); ?></textarea>
344 </td>
345 <td></td>
346 </tr>
347 </tbody>
348 </table>
349 </fieldset>
350 <p></p>
351 <div>
352 <input name="action" type="hidden" value="<?php echo $this->action; ?>">
353 <input name="action2" type="hidden" value="save">
354 <?php wp_nonce_field( 'code_manager_editor', '_wpnonce', false ); ?>
355 <input type="submit" id="submit_button" value="Save changes to database"
356 class="button button-primary" name="submit_button" onclick="return submit_form();">
357 <input type="button" onclick="javascript:location.href='?page=<?php echo CODE_MANAGER_MENU_SLUG; ?>'"
358 class="button button-secondary" value="Back to list">
359 </div>
360 </form>
361 </div>
362 </div>
363 <script type="text/javascript">
364 var wpnone_activate_code_preview = '<?php echo $this->wpnone_activate_code_preview; ?>';
365
366 function submit_form() {
367 if (jQuery('#code_name').val()==='') {
368 alert('Name must be entered');
369 return false;
370 }
371 user_has_edited = false;
372 return true;
373 }
374
375 jQuery(document).ready(function() {
376 jQuery('#code_manager_preview').on('click', function() {
377 if (jQuery('#code_manager_preview').hasClass('dashicons-visibility')) {
378 // Activate preview
379 activate_code();
380
381 jQuery('#code_manager_preview').removeClass('dashicons-visibility').addClass('dashicons-hidden');
382 jQuery('#code_manager_preview').attr('title', '<?php echo $this->code_manager_preview_title_on; ?>');
383 } else {
384 // Deactivate preview
385 deactivate_code();
386
387 jQuery('#code_manager_preview').addClass('dashicons-visibility').removeClass('dashicons-hidden');
388 jQuery('#code_manager_preview').attr('title', '<?php echo $this->code_manager_preview_title_off; ?>');
389 }
390 });
391 });
392 </script>
393 <?php
394 }
395
396 }
397
398 }