wordpress-turnstile

Wordpress paywall plugin
Log | Files | Refs | README | LICENSE

commit 069f7fab5e8b1c86c448e571428f2a658f0c769d
parent b36819e17f09384551a3c7b4566b8d276e692c29
Author: Christian Grothoff <christian@grothoff.org>
Date:   Sun,  2 Nov 2025 13:44:54 +0100

replace stupid machine name prompt by auto-generation

Diffstat:
Madmin/class-price-category-admin.php | 45+++++++++++++++++++++++++++------------------
1 file changed, 27 insertions(+), 18 deletions(-)

diff --git a/admin/class-price-category-admin.php b/admin/class-price-category-admin.php @@ -38,7 +38,6 @@ class Taler_Turnstile_Price_Category_Admin { <thead> <tr> <th><?php esc_html_e('Name', 'taler-turnstile'); ?></th> - <th><?php esc_html_e('Machine Name', 'taler-turnstile'); ?></th> <th><?php esc_html_e('Description', 'taler-turnstile'); ?></th> <th><?php esc_html_e('Actions', 'taler-turnstile'); ?></th> </tr> @@ -47,7 +46,6 @@ class Taler_Turnstile_Price_Category_Admin { <?php foreach ($categories as $id => $category): ?> <tr> <td><strong><?php echo esc_html($category['label']); ?></strong></td> - <td><?php echo esc_html($id); ?></td> <td><?php echo esc_html($category['description'] ?? ''); ?></td> <td> <a href="<?php echo esc_url(admin_url('admin.php?page=taler-price-category-add&edit=' . urlencode($id))); ?>"> @@ -112,18 +110,6 @@ class Taler_Turnstile_Price_Category_Admin { </td> </tr> - <?php if ($is_new): ?> - <tr> - <th scope="row"> - <label for="id"><?php esc_html_e('Machine Name', 'taler-turnstile'); ?> <span class="required">*</span></label> - </th> - <td> - <input type="text" name="id" id="id" value="<?php echo esc_attr($edit_id); ?>" class="regular-text" pattern="[a-z0-9_]+" required> - <p class="description"><?php esc_html_e('Lowercase letters, numbers, and underscores only.', 'taler-turnstile'); ?></p> - </td> - </tr> - <?php endif; ?> - <tr> <th scope="row"> <label for="description"><?php esc_html_e('Description', 'taler-turnstile'); ?></label> @@ -191,14 +177,16 @@ class Taler_Turnstile_Price_Category_Admin { check_admin_referer('taler_save_price_category'); - $id = isset($_POST['id']) ? sanitize_key($_POST['id']) : ''; $label = isset($_POST['label']) ? sanitize_text_field($_POST['label']) : ''; $description = isset($_POST['description']) ? sanitize_textarea_field($_POST['description']) : ''; $prices = isset($_POST['prices']) ? $_POST['prices'] : array(); - // Generate ID from label if creating new + // Determine if this is an edit or new category + $id = isset($_POST['id']) ? sanitize_key($_POST['id']) : ''; + if (empty($id)) { - $id = sanitize_key(str_replace(' ', '_', strtolower($label))); + // Generate a new unique ID + $id = $this->generate_unique_id(); } // Validate and filter prices @@ -223,7 +211,9 @@ class Taler_Turnstile_Price_Category_Admin { Taler_Price_Category::save($id, $category_data); - $message = isset($_POST['id']) ? __('Price category updated.', 'taler-turnstile') : __('Price category created.', 'taler-turnstile'); + $message = isset($_POST['id']) && !empty($_POST['id']) + ? __('Price category updated.', 'taler-turnstile') + : __('Price category created.', 'taler-turnstile'); wp_redirect(add_query_arg( array('page' => 'taler-price-categories', 'message' => urlencode($message)), @@ -253,4 +243,23 @@ class Taler_Turnstile_Price_Category_Admin { )); exit; } + + /** + * Generate a unique ID for a new price category + * + * @return string Unique ID + */ + private function generate_unique_id() { + $categories = Taler_Price_Category::get_all(); + + // Use auto-incrementing numeric IDs starting from 1 + $max_id = 0; + foreach (array_keys($categories) as $existing_id) { + if (is_numeric($existing_id)) { + $max_id = max($max_id, intval($existing_id)); + } + } + + return strval($max_id + 1); + } } \ No newline at end of file