PluginProbe
Tiered Pricing Table for WooCommerce / 6.1.0
Tiered Pricing Table for WooCommerce v6.1.0
8.0.2 7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 2.3.3 All 91 releases
tier-pricing-table / src / Addons / CustomColumns / Form / ColumnsForm.php

ColumnsForm.php in Tiered Pricing Table for WooCommerce 6.1.0, at src/Addons/CustomColumns/Form/ColumnsForm.php

534 lines 17.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace TierPricingTable\Addons\CustomColumns\Form;
2
3 use TierPricingTable\Addons\CustomColumns\CustomColumnsAddon;
4 use TierPricingTable\Addons\CustomColumns\Schema;
5 use TierPricingTable\Core\AdminNotifier;
6 use TierPricingTable\Core\ServiceContainerTrait;
7
8 class ColumnsForm {
9
10 use ServiceContainerTrait;
11
12 const ADD_NEW_ACTION = 'tiered_pricing_add_new_custom_column';
13 const UPDATE_ACTION = 'tiered_pricing_update_custom_column';
14 const REMOVE_COLUMN_ACTION = 'tiered_pricing_remove_custom_column';
15 const REMOVE_ALL_CUSTOM_COLUMNS = 'tiered_pricing_remove_all_custom_columns';
16
17 protected $addon;
18
19 public function __construct( CustomColumnsAddon $addon ) {
20
21 add_action( 'admin_post_' . self::ADD_NEW_ACTION, array( $this, 'handleAddNew' ) );
22 add_action( 'admin_post_' . self::UPDATE_ACTION, array( $this, 'handleUpdate' ) );
23 add_action( 'admin_post_' . self::REMOVE_COLUMN_ACTION, array( $this, 'handleRemove' ) );
24 add_action( 'admin_post_' . self::REMOVE_ALL_CUSTOM_COLUMNS, array( $this, 'handleRemoveAll' ) );
25
26 add_action( 'tiered_pricing_table/settings/table_columns/end', array( $this, 'renderColumnsForm' ) );
27 add_action( 'tiered_pricing_table/settings/table_columns/after_fields', array( $this, 'renderButton' ) );
28
29 $this->addon = $addon;
30 }
31
32 public function handleRemoveAll(): bool {
33
34 $nonce = isset( $_GET['nonce'] ) ? sanitize_text_field( $_GET['nonce'] ) : false;
35
36 if ( ! wp_verify_nonce( $nonce, self::REMOVE_ALL_CUSTOM_COLUMNS ) ) {
37 $this->getContainer()->getAdminNotifier()->flash( __( 'Invalid nonce, please try again',
38 'tier-pricing-table' ), AdminNotifier::ERROR );
39
40 return wp_safe_redirect( wp_get_referer() );
41 }
42
43 foreach ( $this->addon->columnsManager->getColumns() as $column ) {
44 $column->remove();
45 }
46
47 $this->addon->columnsManager->updateRawColumns( array() );
48
49 $this->getContainer()->getAdminNotifier()->flash( __( 'Columns were deleted successfully.',
50 'tier-pricing-table' ) );
51
52 return wp_safe_redirect( wp_get_referer() );
53 }
54
55 public function handleRemove(): bool {
56 $nonce = isset( $_GET['nonce'] ) ? sanitize_text_field( $_GET['nonce'] ) : false;
57
58 if ( ! wp_verify_nonce( $nonce, self::REMOVE_COLUMN_ACTION ) ) {
59 $this->getContainer()->getAdminNotifier()->flash( __( 'Invalid nonce, please try again',
60 'tier-pricing-table' ), AdminNotifier::ERROR );
61
62 return wp_safe_redirect( wp_get_referer() );
63 }
64
65 $slug = ! empty( $_GET['slug'] ) ? sanitize_text_field( $_GET['slug'] ) : false;
66
67 if ( ! $slug ) {
68 $this->getContainer()->getAdminNotifier()->flash( __( 'Invalid column.', 'tier-pricing-table' ),
69 AdminNotifier::ERROR );
70 } else {
71
72 if ( $this->addon->columnsManager->removeColumn( $slug ) ) {
73
74 $this->getContainer()->getAdminNotifier()->flash( __( 'Column removed successfully.',
75 'tier-pricing-table' ) );
76 } else {
77 $this->getContainer()->getAdminNotifier()->flash( __( 'Something went wrong. Please try again.',
78 'tier-pricing-table' ), AdminNotifier::ERROR );
79 }
80 }
81
82 return wp_safe_redirect( wp_get_referer() );
83 }
84
85 public function handleAddNew(): bool {
86 $nonce = isset( $_GET['nonce'] ) ? sanitize_text_field( $_GET['nonce'] ) : false;
87
88 if ( ! wp_verify_nonce( $nonce, self::ADD_NEW_ACTION ) ) {
89 $this->getContainer()->getAdminNotifier()->flash( __( 'Invalid nonce, please try again',
90 'tier-pricing-table' ), AdminNotifier::ERROR );
91
92 return wp_safe_redirect( wp_get_referer() );
93 }
94
95 $name = ! empty( $_GET['name'] ) ? sanitize_text_field( $_GET['name'] ) : false;
96 $type = ! empty( $_GET['type'] ) ? sanitize_text_field( $_GET['type'] ) : false;
97 $dataType = ! empty ( $_GET['data_type'] ) ? sanitize_text_field( $_GET['data_type'] ) : false;
98
99 $slug = strtolower( wp_generate_password( 10, false ) );
100
101 $columnInstance = $this->addon->columnsManager->getColumnInstance( $slug, array(
102 'name' => $name,
103 'type' => $type,
104 'data_type' => $dataType,
105 ) );
106
107 try {
108 if ( $columnInstance && $columnInstance->save() ) {
109 $this->getContainer()->getAdminNotifier()->flash( __( 'Column added successfully.',
110 'tier-pricing-table' ) );
111 } else {
112 $this->getContainer()->getAdminNotifier()->flash( __( 'Something went wrong. Please try again.',
113 'tier-pricing-table' ), AdminNotifier::ERROR );
114 }
115 } catch ( \Exception $e ) {
116 $this->getContainer()->getAdminNotifier()->flash( __( 'Invalid column data.', 'tier-pricing-table' ),
117 AdminNotifier::ERROR );
118 }
119
120 return wp_safe_redirect( wp_get_referer() );
121 }
122
123 public function handleUpdate(): bool {
124 $nonce = isset( $_GET['nonce'] ) ? sanitize_text_field( $_GET['nonce'] ) : false;
125
126 if ( ! wp_verify_nonce( $nonce, self::UPDATE_ACTION ) ) {
127 $this->getContainer()->getAdminNotifier()->flash( __( 'Invalid nonce, please try again',
128 'tier-pricing-table' ), AdminNotifier::ERROR );
129
130 return wp_safe_redirect( wp_get_referer() );
131 }
132
133 $slug = ! empty( $_GET['slug'] ) ? sanitize_text_field( $_GET['slug'] ) : false;
134 $name = ! empty( $_GET['name'] ) ? sanitize_text_field( $_GET['name'] ) : false;
135 $type = ! empty( $_GET['type'] ) ? sanitize_text_field( $_GET['type'] ) : false;
136 $dataType = ! empty ( $_GET['data_type'] ) ? sanitize_text_field( $_GET['data_type'] ) : false;
137
138 $columnInstance = $this->addon->columnsManager->getColumnInstance( $slug, array(
139 'name' => $name,
140 'type' => $type,
141 'data_type' => $dataType,
142 ) );
143
144 try {
145 if ( $columnInstance && $columnInstance->save() ) {
146 $this->getContainer()->getAdminNotifier()->flash( __( 'Column edited successfully.',
147 'tier-pricing-table' ) );
148 } else {
149 $this->getContainer()->getAdminNotifier()->flash( __( 'Something went wrong. Please try again.',
150 'tier-pricing-table' ), AdminNotifier::ERROR );
151 }
152 } catch ( \Exception $e ) {
153 $this->getContainer()->getAdminNotifier()->flash( __( 'Invalid column data.', 'tier-pricing-table' ),
154 AdminNotifier::ERROR );
155 }
156
157 return wp_safe_redirect( wp_get_referer() );
158 }
159
160 public function renderButton() {
161 ?>
162
163 <?php foreach ( $this->addon->columnsManager->getColumns() as $column ) : ?>
164 <div>
165 <label for="tiered_pricing_custom_column_<?php echo esc_attr( $column->getSlug() ); ?>">
166 <?php echo esc_attr( $column->getName() ); ?>:
167 </label>
168 <br>
169 <input disabled
170 type="text" style="width: 190px;"
171 id="tiered_pricing_custom_column_<?php echo esc_attr( $column->getSlug() ); ?>"
172 placeholder="<?php echo esc_attr( $column->getFormattedType() ); ?>">
173 </div>
174 <?php endforeach; ?>
175
176 <div>
177 <button type="button" class="button button-tpt-button tiered-pricing-custom-columns-show-button">
178 <?php esc_html_e( 'Manage custom columns', 'tier-pricing-table' ); ?>
179 </button>
180 </div>
181
182 <?php
183 }
184
185 public function renderColumnsForm() {
186 ?>
187 <script>
188 jQuery(document).ready(function () {
189 const CustomColumnsTable = function (container) {
190
191 this.init = function () {
192 jQuery('.tiered-pricing-custom-columns-show-button').click(function (e) {
193 e.preventDefault();
194 container.toggleClass('tiered-pricing-custom-columns-table--hidden');
195 });
196
197 jQuery('.tiered-pricing-custom-columns-table__new-column-row button').click((function (e) {
198
199 e.preventDefault();
200
201 const _container = container.find('.tiered-pricing-custom-columns-table__new-column-row');
202
203 const name = _container.find('[name=tiered_pricing_custom_column_name]').val();
204 const type = _container.find('[name=tiered_pricing_custom_column_type]').val();
205 const dataType = _container.find('[name=tiered_pricing_custom_column_data_type]').val();
206
207 if (!name) {
208 alert('Please enter the column name');
209
210 return;
211 }
212
213 this.request(this.getAddNewURL(), {
214 'name': name,
215 'type': type,
216 'data_type': dataType
217 });
218
219 }).bind(this));
220
221 jQuery('.tiered-pricing-custom-columns-table__edit-button').click((function (e) {
222
223 e.preventDefault();
224
225 const _container = jQuery(e.target).closest('tr');
226
227 const slug = _container.data('slug');
228 const name = _container.find('[name=tiered_pricing_custom_column_name]').val();
229 const type = _container.find('[name=tiered_pricing_custom_column_type]').val();
230 const dataType = _container.find('[name=tiered_pricing_custom_column_data_type]').val();
231
232 if (!name) {
233 alert('Please enter the column name');
234
235 return;
236 }
237
238 this.request(this.getUpdateURL(), {
239 'slug': slug,
240 'name': name,
241 'type': type,
242 'data_type': dataType
243 });
244 }).bind(this));
245
246 jQuery('[name=tiered_pricing_custom_column_type]').change(function () {
247
248 const container = jQuery(this).closest('tr');
249
250 const val = jQuery(this).val();
251
252 if (val !== 'custom') {
253 container.find('[name=tiered_pricing_custom_column_data_type]').parent().hide();
254 } else {
255 container.find('[name=tiered_pricing_custom_column_data_type]').parent().show();
256 }
257 }).trigger('change');
258 }
259
260 this.request = function (_URL, params) {
261 let url = new URL(_URL);
262
263 for (let name in params) {
264 url.searchParams.append(name, params[name]);
265 }
266
267 window.location.href = url.href;
268 }
269
270 this.getAddNewURL = function () {
271 return container.data('add-new-url');
272 }
273 this.getUpdateURL = function () {
274 return container.data('update-url');
275 }
276 }
277
278 const customColumnsTable = new CustomColumnsTable(jQuery('.tiered-pricing-custom-columns-table'));
279 customColumnsTable.init();
280 });
281 </script>
282 <style>
283 .tiered-pricing-custom-columns-table {
284 margin-top: 20px;
285 }
286
287 .tiered-pricing-custom-columns-table table {
288 width: max-content;
289 }
290
291 .tiered-pricing-custom-columns-table--hidden {
292 display: none;
293 }
294
295 .tiered-pricing-custom-columns-table table th, .tiered-pricing-custom-columns-table table td {
296 padding: 20px;
297 }
298 </style>
299 <?php
300 $addNewURL = add_query_arg( array(
301 'action' => self::ADD_NEW_ACTION,
302 'nonce' => wp_create_nonce( self::ADD_NEW_ACTION ),
303 ), admin_url( 'admin-post.php' ) );
304
305 $updateURL = add_query_arg( array(
306 'action' => self::UPDATE_ACTION,
307 'nonce' => wp_create_nonce( self::UPDATE_ACTION ),
308 ), admin_url( 'admin-post.php' ) )
309
310 ?>
311 <div class="tiered-pricing-custom-columns-table tiered-pricing-custom-columns-table--hidden"
312 data-add-new-url="<?php echo esc_attr( $addNewURL ); ?>"
313 data-update-url=" <?php echo esc_attr( $updateURL ); ?>">
314 <table class="wp-list-table widefat fixed striped">
315 <thead>
316 <tr style="text-align: center">
317 <th><?php esc_html_e( 'Column name', 'tier-pricing-table' ); ?></th>
318 <th><?php esc_html_e( 'Column content', 'tier-pricing-table' ); ?></th>
319 <th><?php esc_html_e( 'Content type', 'tier-pricing-table' ); ?></th>
320 <th><?php esc_html_e( 'Actions', 'tier-pricing-table' ); ?></th>
321 </tr>
322 </thead>
323 <tbody>
324 <?php foreach ( $this->addon->columnsManager->getColumns() as $column ) : ?>
325 <tr data-slug="<?php echo esc_attr( $column->getSlug() ); ?>">
326 <td>
327 <input style="width: 100%"
328 name="tiered_pricing_custom_column_name"
329 type="text"
330 value="<?php echo esc_attr( $column->getName() ); ?>">
331 </td>
332 <td>
333 <select style="width: 100%"
334 name="tiered_pricing_custom_column_type">
335 <?php foreach ( Schema::getAvailableCustomColumnsTypes() as $key => $label ) : ?>
336 <option value="<?php echo esc_attr( $key ); ?>"
337 <?php
338 selected( $column->getType(), $key );
339 ?>
340 >
341 <?php echo esc_html( $label ); ?>
342 </option>
343 <?php endforeach; ?>
344 </select>
345 </td>
346 <td>
347 <div>
348 <select style="width: 100%"
349 name="tiered_pricing_custom_column_data_type">
350 <?php foreach ( Schema::getAvailableDataTypes() as $key => $label ) : ?>
351 <option
352 value="<?php echo esc_attr( $key ); ?>"
353 <?php
354 selected( $column->getDataType(), $key );
355 ?>
356 >
357 <?php echo esc_html( $label ); ?>
358 </option>
359 <?php endforeach; ?>
360 </select>
361 </div>
362
363 </td>
364 <td>
365 <button class="button button-tpt-button tiered-pricing-custom-columns-table__edit-button">
366 <?php esc_html_e( 'Update', 'tier-pricing-table' ); ?>
367 </button>
368 <?php
369 $removeURL = add_query_arg( array(
370 'nonce' => wp_create_nonce( self::REMOVE_COLUMN_ACTION ),
371 'action' => self::REMOVE_COLUMN_ACTION,
372 'slug' => $column->getSlug(),
373 ), admin_url( 'admin-post.php' ) );
374 ?>
375
376 <a class="button"
377 type="button"
378 href="<?php echo esc_attr( $removeURL ); ?>"
379 onclick="return confirm('Are you sure? All column\'s data will be deleted.');"
380 data-slug="<?php echo esc_attr( $column->getSlug() ); ?>" style="color: red !important;
381 border-color: red !important;">
382 <?php esc_html_e( 'Delete', 'tier-pricing-table' ); ?>
383 </a>
384 </td>
385 </tr>
386 <?php endforeach; ?>
387 </tbody>
388 <tfoot>
389 <tr class="tiered-pricing-custom-columns-table__new-column-row" style="background: #e9e6ed;">
390 <td style="vertical-align: top">
391 <input type="text"
392 style="width: 100%"
393 placeholder="<?php esc_attr_e( 'Enter column name', 'tier-pricing-table' ); ?>"
394 name="tiered_pricing_custom_column_name">
395 <div style="margin-top: 5px;">
396 <ul>
397 <li>
398 <?php
399 esc_html_e( 'Enter the name of the column.',
400 'tier-pricing-table' );
401 ?>
402 </li>
403 </ul>
404 </div>
405 </td>
406 <td style="vertical-align: top">
407 <select name="tiered_pricing_custom_column_type" style="width: 100%">
408 <?php foreach ( Schema::getAvailableCustomColumnsTypes() as $key => $label ) : ?>
409 <option value="<?php echo esc_attr( $key ); ?>">
410 <?php echo esc_html( $label ); ?>
411 </option>
412 <?php endforeach; ?>
413 </select>
414 <div style="margin-top: 5px;">
415 <ul>
416 <li>
417 <b><?php esc_html_e( 'Custom column', 'tier-pricing-table' ); ?></b>
418 <?php
419 esc_html_e( 'adds additional input to tiered pricing form where you can input custom content for each tier.',
420 'tier-pricing-table' );
421 ?>
422 </li>
423 <li>
424 <b><?php esc_html_e( 'Price excluding taxes', 'tier-pricing-table' ); ?></b>
425 <?php
426 esc_html_e( 'shows the price excluding taxes for each tier.',
427 'tier-pricing-table' );
428 ?>
429 </li>
430 <li>
431 <b><?php esc_html_e( 'Price including taxes', 'tier-pricing-table' ); ?></b>
432 <?php
433 esc_html_e( 'shows the price including taxes for each tier.',
434 'tier-pricing-table' );
435 ?>
436 </li>
437 <li>
438 <b><?php esc_html_e( 'Total row price', 'tier-pricing-table' ); ?></b>
439 <?php
440 esc_html_e( 'shows the total price for each tier.', 'tier-pricing-table' );
441 ?>
442 </li>
443 </ul>
444 </div>
445 </td>
446 <td style="vertical-align: top">
447
448 <div>
449 <select name="tiered_pricing_custom_column_data_type" style="width: 100%">
450 <?php foreach ( Schema::getAvailableDataTypes() as $key => $label ) : ?>
451 <option value="<?php echo esc_attr( $key ); ?>">
452 <?php echo esc_html( $label ); ?>
453 </option>
454 <?php endforeach; ?>
455 </select>
456 <div style="margin-top: 5px;">
457 <?php
458 esc_html_e( 'Choose how to format content from your custom column:',
459 'tier-pricing-table' );
460 ?>
461 <ul>
462 <li>
463 <b><?php esc_html_e( 'Price', 'tier-pricing-table' ); ?>:</b>
464 <?php
465 esc_html_e( 'format the content as a price.', 'tier-pricing-table' );
466 ?>
467 </li>
468 <li>
469 <b><?php esc_html_e( 'Number', 'tier-pricing-table' ); ?>:</b>
470 <?php
471 esc_html_e( 'format the content as a number.', 'tier-pricing-table' );
472 ?>
473 </li>
474 <li>
475 <b><?php esc_html_e( 'Text', 'tier-pricing-table' ); ?>:</b>
476 <?php
477 esc_html_e( 'don\'t format the content. Display it as is.',
478 'tier-pricing-table' );
479 ?>
480 </li>
481 </ul>
482
483 </div>
484 </div>
485 </td>
486
487
488 <td style="vertical-align: top">
489 <button class="button button-tpt-button" type="button">
490 <span style="font-size: 1.2em; line-height: 28px;" class="dashicons dashicons-plus-alt">
491 </span>
492 <?php esc_html_e( 'Add new column', 'tier-pricing-table' ); ?>
493 </button>
494 </td>
495 </tr>
496
497 </tfoot>
498 </table>
499 <?php if ( ! tpt_fs()->can_use_premium_code() ) : ?>
500 <div style="margin-top: 10px">
501 <span style="color:red">
502 <?php esc_html_e( 'Available only in the premium version.', 'tier-pricing-table' ); ?>
503 </span>
504 <a type="button"
505 target="_blank"
506 href="<?php echo esc_attr( tpt_fs()->get_upgrade_url() ); ?>">
507 <?php esc_html_e( 'Upgrade your plan', 'tier-pricing-table' ); ?>
508 </a>
509 </div>
510 <?php endif; ?>
511
512
513 <?php if ( ! empty( $this->addon->columnsManager->getRawColumns() ) ) : ?>
514
515 <?php
516 $removeAllURL = add_query_arg( array(
517 'nonce' => wp_create_nonce( self::REMOVE_ALL_CUSTOM_COLUMNS ),
518 'action' => self::REMOVE_ALL_CUSTOM_COLUMNS,
519 ), admin_url( 'admin-post.php' ) )
520 ?>
521
522 <div style="margin-top: 10px">
523 <a style="color:red"
524 type="button"
525 onclick="return confirm('Are you sure? All columns\' data will be deleted.');"
526 href=" <?php echo esc_attr( $removeAllURL ); ?> ">
527 <?php esc_html_e( 'Remove all custom columns', 'tier-pricing-table' ); ?>
528 </a>
529 </div>
530 <?php endif; ?>
531 </div>
532 <?php
533 }
534 }