| 1 |
<?php |
| 2 |
/** |
| 3 |
* BotWriter Templates Management |
| 4 |
* |
| 5 |
* Handles CRUD operations for prompt templates and provides the admin UI |
| 6 |
*/ |
| 7 |
|
| 8 |
if (!defined('ABSPATH')) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Render the templates management page |
| 14 |
*/ |
| 15 |
function botwriter_templates_page_handler() { |
| 16 |
if (!current_user_can('manage_options')) { |
| 17 |
return; |
| 18 |
} |
| 19 |
|
| 20 |
global $wpdb; |
| 21 |
$table_name = $wpdb->prefix . 'botwriter_templates'; |
| 22 |
|
| 23 |
// Handle form submissions |
| 24 |
$message = ''; |
| 25 |
$message_type = 'updated'; |
| 26 |
|
| 27 |
// Handle set default action |
| 28 |
if (isset($_GET['action']) && sanitize_text_field(wp_unslash($_GET['action'])) === 'set_default' && isset($_GET['template_id'])) { |
| 29 |
if (!wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['_wpnonce'] ?? '')), 'botwriter_set_default_template')) { |
| 30 |
wp_die(esc_html__('Security check failed', 'botwriter')); |
| 31 |
} |
| 32 |
|
| 33 |
$template_id = intval($_GET['template_id']); |
| 34 |
$result = botwriter_set_default_template($template_id); |
| 35 |
|
| 36 |
if ($result !== false) { |
| 37 |
$message = __('Default template updated successfully.', 'botwriter'); |
| 38 |
} else { |
| 39 |
$message = __('Error setting default template.', 'botwriter'); |
| 40 |
$message_type = 'error'; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
// Handle delete action |
| 45 |
if (isset($_GET['action']) && sanitize_text_field(wp_unslash($_GET['action'])) === 'delete' && isset($_GET['template_id'])) { |
| 46 |
if (!wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['_wpnonce'] ?? '')), 'botwriter_delete_template')) { |
| 47 |
wp_die(esc_html__('Security check failed', 'botwriter')); |
| 48 |
} |
| 49 |
|
| 50 |
$template_id = intval($_GET['template_id']); |
| 51 |
$result = botwriter_delete_template($template_id); |
| 52 |
|
| 53 |
if ($result === false) { |
| 54 |
$message = __('Cannot delete the default template.', 'botwriter'); |
| 55 |
$message_type = 'error'; |
| 56 |
} else { |
| 57 |
$message = __('Template deleted successfully.', 'botwriter'); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
// Handle save action |
| 62 |
if (isset($_POST['botwriter_save_template']) && isset($_POST['_wpnonce'])) { |
| 63 |
if (!wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wpnonce'])), 'botwriter_save_template')) { |
| 64 |
wp_die(esc_html__('Security check failed', 'botwriter')); |
| 65 |
} |
| 66 |
|
| 67 |
$template_data = [ |
| 68 |
'id' => isset($_POST['template_id']) ? intval($_POST['template_id']) : 0, |
| 69 |
'name' => isset($_POST['template_name']) ? sanitize_text_field(wp_unslash($_POST['template_name'])) : '', |
| 70 |
'content' => isset($_POST['template_content']) ? wp_kses_post(wp_unslash($_POST['template_content'])) : '' |
| 71 |
]; |
| 72 |
|
| 73 |
if (empty($template_data['name'])) { |
| 74 |
$message = __('Template name is required.', 'botwriter'); |
| 75 |
$message_type = 'error'; |
| 76 |
} else { |
| 77 |
$result = botwriter_save_template($template_data); |
| 78 |
if ($result !== false) { |
| 79 |
$message = __('Template saved successfully.', 'botwriter'); |
| 80 |
} else { |
| 81 |
$message = __('Error saving template.', 'botwriter'); |
| 82 |
$message_type = 'error'; |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// Check if we're editing a template |
| 88 |
$editing_template = null; |
| 89 |
if (isset($_GET['action']) && $_GET['action'] === 'edit' && isset($_GET['template_id'])) { |
| 90 |
$editing_template = botwriter_get_template(intval($_GET['template_id'])); |
| 91 |
} |
| 92 |
|
| 93 |
// Get all templates |
| 94 |
$templates = botwriter_get_all_templates(); |
| 95 |
|
| 96 |
// Ensure default template exists |
| 97 |
if (empty($templates)) { |
| 98 |
botwriter_insert_default_template(); |
| 99 |
$templates = botwriter_get_all_templates(); |
| 100 |
} |
| 101 |
|
| 102 |
?> |
| 103 |
<div class="wrap"> |
| 104 |
<h1 class="wp-heading-inline"><?php esc_html_e('Prompt Templates', 'botwriter'); ?></h1> |
| 105 |
<a href="<?php echo esc_url(admin_url('admin.php?page=botwriter_templates&action=new')); ?>" class="page-title-action"> |
| 106 |
<?php esc_html_e('Add New', 'botwriter'); ?> |
| 107 |
</a> |
| 108 |
<hr class="wp-header-end"> |
| 109 |
|
| 110 |
<?php if ($message): ?> |
| 111 |
<div class="notice notice-<?php echo esc_attr($message_type === 'error' ? 'error' : 'success'); ?> is-dismissible"> |
| 112 |
<p><?php echo esc_html($message); ?></p> |
| 113 |
</div> |
| 114 |
<?php endif; ?> |
| 115 |
|
| 116 |
<?php if (isset($_GET['action']) && in_array($_GET['action'], ['new', 'edit'])): ?> |
| 117 |
<!-- Template Editor Form --> |
| 118 |
<?php botwriter_render_template_editor($editing_template); ?> |
| 119 |
<?php else: ?> |
| 120 |
<!-- Templates List --> |
| 121 |
<?php botwriter_render_templates_list($templates); ?> |
| 122 |
<?php endif; ?> |
| 123 |
|
| 124 |
<!-- Available Placeholders Reference --> |
| 125 |
<div class="card" style="max-width: 100%; margin-top: 20px;"> |
| 126 |
<h2><?php esc_html_e('Available Placeholders', 'botwriter'); ?></h2> |
| 127 |
<p><?php esc_html_e('Use these placeholders in your templates. They will be replaced with actual values when generating content.', 'botwriter'); ?></p> |
| 128 |
|
| 129 |
<table class="widefat striped"> |
| 130 |
<thead> |
| 131 |
<tr> |
| 132 |
<th><?php esc_html_e('Placeholder', 'botwriter'); ?></th> |
| 133 |
<th><?php esc_html_e('Description', 'botwriter'); ?></th> |
| 134 |
</tr> |
| 135 |
</thead> |
| 136 |
<tbody> |
| 137 |
<tr> |
| 138 |
<td><code>{{post_length}}</code></td> |
| 139 |
<td><?php esc_html_e('Number of words for the article (e.g., 800, 1200)', 'botwriter'); ?></td> |
| 140 |
</tr> |
| 141 |
<tr> |
| 142 |
<td><code>{{post_language}}</code></td> |
| 143 |
<td><?php esc_html_e('Language name (e.g., English, Spanish)', 'botwriter'); ?></td> |
| 144 |
</tr> |
| 145 |
<tr> |
| 146 |
<td><code>{{writer_style}}</code></td> |
| 147 |
<td><?php esc_html_e('Writing style based on selected writer persona', 'botwriter'); ?></td> |
| 148 |
</tr> |
| 149 |
<tr> |
| 150 |
<td><code>{{prompt_or_keywords}}</code></td> |
| 151 |
<td><?php esc_html_e('User-provided prompt or keywords for content generation', 'botwriter'); ?></td> |
| 152 |
</tr> |
| 153 |
</tbody> |
| 154 |
</table> |
| 155 |
</div> |
| 156 |
</div> |
| 157 |
<?php |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Render the template editor form |
| 162 |
*/ |
| 163 |
function botwriter_render_template_editor($template = null) { |
| 164 |
$is_new = empty($template); |
| 165 |
$is_default = !$is_new && !empty($template['is_default']); |
| 166 |
?> |
| 167 |
<form method="post" action=""> |
| 168 |
<?php wp_nonce_field('botwriter_save_template'); ?> |
| 169 |
|
| 170 |
<?php if (!$is_new): ?> |
| 171 |
<input type="hidden" name="template_id" value="<?php echo esc_attr($template['id']); ?>"> |
| 172 |
<?php endif; ?> |
| 173 |
|
| 174 |
<table class="form-table"> |
| 175 |
<tr> |
| 176 |
<th scope="row"> |
| 177 |
<label for="template_name"><?php esc_html_e('Template Name', 'botwriter'); ?></label> |
| 178 |
</th> |
| 179 |
<td> |
| 180 |
<input type="text" |
| 181 |
name="template_name" |
| 182 |
id="template_name" |
| 183 |
class="regular-text" |
| 184 |
value="<?php echo esc_attr($template['name'] ?? ''); ?>" |
| 185 |
<?php echo $is_default ? 'readonly' : ''; ?> |
| 186 |
required> |
| 187 |
<?php if ($is_default): ?> |
| 188 |
<p class="description"><?php esc_html_e('Default template name cannot be changed.', 'botwriter'); ?></p> |
| 189 |
<?php endif; ?> |
| 190 |
</td> |
| 191 |
</tr> |
| 192 |
<tr> |
| 193 |
<th scope="row"> |
| 194 |
<label for="template_content"><?php esc_html_e('Template Content', 'botwriter'); ?></label> |
| 195 |
</th> |
| 196 |
<td> |
| 197 |
<textarea name="template_content" |
| 198 |
id="template_content" |
| 199 |
rows="20" |
| 200 |
class="large-text code" |
| 201 |
style="font-family: monospace; width: 100%;"><?php echo esc_textarea($template['content'] ?? botwriter_get_default_template_content()); ?></textarea> |
| 202 |
<p class="description"> |
| 203 |
<?php esc_html_e('Use placeholders like {{post_length}} to insert dynamic values. See the reference below.', 'botwriter'); ?> |
| 204 |
</p> |
| 205 |
</td> |
| 206 |
</tr> |
| 207 |
</table> |
| 208 |
|
| 209 |
<p class="submit"> |
| 210 |
<input type="submit" |
| 211 |
name="botwriter_save_template" |
| 212 |
class="button button-primary" |
| 213 |
value="<?php echo $is_new ? esc_attr__('Create Template', 'botwriter') : esc_attr__('Save Changes', 'botwriter'); ?>"> |
| 214 |
<a href="<?php echo esc_url(admin_url('admin.php?page=botwriter_templates')); ?>" class="button"> |
| 215 |
<?php esc_html_e('Cancel', 'botwriter'); ?> |
| 216 |
</a> |
| 217 |
</p> |
| 218 |
</form> |
| 219 |
<?php |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Render the templates list table |
| 224 |
*/ |
| 225 |
function botwriter_render_templates_list($templates) { |
| 226 |
?> |
| 227 |
<table class="wp-list-table widefat fixed striped"> |
| 228 |
<thead> |
| 229 |
<tr> |
| 230 |
<th scope="col" class="manage-column"><?php esc_html_e('Name', 'botwriter'); ?></th> |
| 231 |
<th scope="col" class="manage-column"><?php esc_html_e('Type', 'botwriter'); ?></th> |
| 232 |
<th scope="col" class="manage-column"><?php esc_html_e('Created', 'botwriter'); ?></th> |
| 233 |
<th scope="col" class="manage-column"><?php esc_html_e('Actions', 'botwriter'); ?></th> |
| 234 |
</tr> |
| 235 |
</thead> |
| 236 |
<tbody> |
| 237 |
<?php if (empty($templates)): ?> |
| 238 |
<tr> |
| 239 |
<td colspan="4"><?php esc_html_e('No templates found.', 'botwriter'); ?></td> |
| 240 |
</tr> |
| 241 |
<?php else: ?> |
| 242 |
<?php foreach ($templates as $template): ?> |
| 243 |
<tr> |
| 244 |
<td> |
| 245 |
<strong> |
| 246 |
<a href="<?php echo esc_url(admin_url('admin.php?page=botwriter_templates&action=edit&template_id=' . $template['id'])); ?>"> |
| 247 |
<?php echo esc_html($template['name']); ?> |
| 248 |
</a> |
| 249 |
</strong> |
| 250 |
</td> |
| 251 |
<td> |
| 252 |
<?php if ($template['is_default']): ?> |
| 253 |
<span class="dashicons dashicons-star-filled" style="color: #f0c33c;"></span> |
| 254 |
<?php esc_html_e('Default', 'botwriter'); ?> |
| 255 |
<?php else: ?> |
| 256 |
<?php esc_html_e('Custom', 'botwriter'); ?> |
| 257 |
<?php endif; ?> |
| 258 |
</td> |
| 259 |
<td> |
| 260 |
<?php echo esc_html(date_i18n(get_option('date_format'), strtotime($template['created_at']))); ?> |
| 261 |
</td> |
| 262 |
<td> |
| 263 |
<a href="<?php echo esc_url(admin_url('admin.php?page=botwriter_templates&action=edit&template_id=' . $template['id'])); ?>" class="button button-small"> |
| 264 |
<?php esc_html_e('Edit', 'botwriter'); ?> |
| 265 |
</a> |
| 266 |
<?php if (!$template['is_default']): ?> |
| 267 |
<a href="<?php echo esc_url(wp_nonce_url(admin_url('admin.php?page=botwriter_templates&action=set_default&template_id=' . $template['id']), 'botwriter_set_default_template')); ?>" |
| 268 |
class="button button-small button-primary"> |
| 269 |
<?php esc_html_e('Set as Default', 'botwriter'); ?> |
| 270 |
</a> |
| 271 |
<a href="<?php echo esc_url(wp_nonce_url(admin_url('admin.php?page=botwriter_templates&action=delete&template_id=' . $template['id']), 'botwriter_delete_template')); ?>" |
| 272 |
class="button button-small button-link-delete botwriter-template-delete" |
| 273 |
data-confirm="<?php esc_attr_e('Are you sure you want to delete this template?', 'botwriter'); ?>"> |
| 274 |
<?php esc_html_e('Delete', 'botwriter'); ?> |
| 275 |
</a> |
| 276 |
<?php endif; ?> |
| 277 |
</td> |
| 278 |
</tr> |
| 279 |
<?php endforeach; ?> |
| 280 |
<?php endif; ?> |
| 281 |
</tbody> |
| 282 |
</table> |
| 283 |
<?php |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Get templates for dropdown select in task forms |
| 288 |
*/ |
| 289 |
function botwriter_get_templates_dropdown_options($selected_id = null) { |
| 290 |
$templates = botwriter_get_all_templates(); |
| 291 |
$options = '<option value="">' . esc_html__('-- Use Default Template --', 'botwriter') . '</option>'; |
| 292 |
|
| 293 |
foreach ($templates as $template) { |
| 294 |
$selected = ($selected_id && $selected_id == $template['id']) ? 'selected' : ''; |
| 295 |
$label = $template['name']; |
| 296 |
if ($template['is_default']) { |
| 297 |
$label .= ' (' . __('Default', 'botwriter') . ')'; |
| 298 |
} |
| 299 |
$options .= sprintf( |
| 300 |
'<option value="%d" %s>%s</option>', |
| 301 |
esc_attr($template['id']), |
| 302 |
$selected, |
| 303 |
esc_html($label) |
| 304 |
); |
| 305 |
} |
| 306 |
|
| 307 |
return $options; |
| 308 |
} |
| 309 |
|