Wheel_of_Fortune_Plugin/admin/wheels-page.php

105 lines
5.7 KiB
PHP

<?php
/**
* Admin page for managing multiple wheels
*/
if (!defined('ABSPATH')) exit;
global $wpdb;
$wheels_table = $wpdb->prefix . 'wof_wheels';
// Handle adding a new wheel
if (isset($_POST['add_wheel_submit']) && check_admin_referer('wof_add_wheel_nonce')) {
$wheel_name = sanitize_text_field($_POST['wheel_name']);
$wheel_slug = sanitize_title($_POST['wheel_name']); // Generate slug from name
if (!empty($wheel_name)) {
$wpdb->insert(
$wheels_table,
['name' => $wheel_name, 'slug' => $wheel_slug, 'created_at' => current_time('mysql')],
['%s', '%s', '%s']
);
echo '<div class="notice notice-success is-dismissible"><p>' . __('New wheel created successfully!', 'wheel-of-fortune') . '</p></div>';
} else {
echo '<div class="notice notice-error is-dismissible"><p>' . __('Please provide a name for the wheel.', 'wheel-of-fortune') . '</p></div>';
}
}
// Handle deleting a wheel
if (isset($_GET['action']) && $_GET['action'] === 'delete' && isset($_GET['wheel_id']) && check_admin_referer('wof_delete_wheel_' . $_GET['wheel_id'])) {
$wheel_id_to_delete = intval($_GET['wheel_id']);
if ($wheel_id_to_delete !== 1) { // Prevent deleting the default wheel
// TODO: Consider what to do with prizes associated with this wheel. For now, we'll just delete the wheel.
$wpdb->delete($wheels_table, ['id' => $wheel_id_to_delete], ['%d']);
echo '<div class="notice notice-success is-dismissible"><p>' . __('Wheel deleted successfully.', 'wheel-of-fortune') . '</p></div>';
} else {
echo '<div class="notice notice-error is-dismissible"><p>' . __('The default wheel cannot be deleted.', 'wheel-of-fortune') . '</p></div>';
}
}
$wheels = $wpdb->get_results("SELECT * FROM $wheels_table ORDER BY id ASC", ARRAY_A);
?>
<div class="wrap wheel-admin-page">
<h1><?php echo esc_html__('Wheels of Fortune', 'wheel-of-fortune'); ?></h1>
<div id="col-container" class="wp-clearfix">
<div id="col-left">
<div class="col-wrap">
<div class="form-wrap">
<h2><?php echo esc_html__('Add New Wheel', 'wheel-of-fortune'); ?></h2>
<form method="post" action="">
<?php wp_nonce_field('wof_add_wheel_nonce'); ?>
<div class="form-field">
<label for="wheel_name"><?php _e('Wheel Name', 'wheel-of-fortune'); ?></label>
<input type="text" name="wheel_name" id="wheel_name" required>
<p><?php _e('The name is how it appears on your site.', 'wheel-of-fortune'); ?></p>
</div>
<p class="submit">
<input type="submit" name="add_wheel_submit" id="submit" class="button button-primary" value="<?php _e('Add New Wheel', 'wheel-of-fortune'); ?>">
</p>
</form>
</div>
</div>
</div>
<div id="col-right">
<div class="col-wrap">
<table class="wp-list-table widefat fixed striped">
<thead>
<tr>
<th scope="col" class="manage-column"><?php _e('ID', 'wheel-of-fortune'); ?></th>
<th scope="col" class="manage-column"><?php _e('Name', 'wheel-of-fortune'); ?></th>
<th scope="col" class="manage-column"><?php _e('Shortcode', 'wheel-of-fortune'); ?></th>
<th scope="col" class="manage-column"><?php _e('Actions', 'wheel-of-fortune'); ?></th>
</tr>
</thead>
<tbody>
<?php if (!empty($wheels)): ?>
<?php foreach ($wheels as $wheel): ?>
<tr>
<td><?php echo esc_html($wheel['id']); ?></td>
<td>
<strong><a href="<?php echo admin_url('admin.php?page=wof-edit-wheel&wheel_id=' . $wheel['id']); ?>"><?php echo esc_html($wheel['name']); ?></a></strong>
</td>
<td>
<code>[wheel_of_fortune id="<?php echo esc_attr($wheel['slug']); ?>"]</code>
</td>
<td>
<a href="<?php echo admin_url('admin.php?page=wof-edit-wheel&wheel_id=' . $wheel['id']); ?>"><?php _e('Edit', 'wheel-of-fortune'); ?></a>
<?php if ($wheel['id'] != 1): // Cannot delete default wheel ?>
| <a href="<?php echo wp_nonce_url(admin_url('admin.php?page=wof-wheels&action=delete&wheel_id=' . $wheel['id']), 'wof_delete_wheel_' . $wheel['id']); ?>" class="delete-link" style="color:red;" onclick="return confirm('<?php _e('Are you sure you want to delete this wheel? This cannot be undone.', 'wheel-of-fortune'); ?>');"><?php _e('Delete', 'wheel-of-fortune'); ?></a>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="4"><?php _e('No wheels found.', 'wheel-of-fortune'); ?></td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>