Repository navigation
Expand file tree
/
Copy pathadmin-notification-page.php
More file actions
147 lines (137 loc) · 6.33 KB
/
Copy pathadmin-notification-page.php
File metadata and controls
147 lines (137 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
if (!defined('ABSPATH')) {
die;
}
global $wpdb;
$table_name = $wpdb->prefix . 'jw_notifications';
$action = isset($_GET['action']) ? $_GET['action'] : '';
$id = isset($_GET['id']) ? absint($_GET['id']) : 0;
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['jw_notification_save'])) {
if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'jw_notification_save_nonce')) {
wp_die(esc_html__('Security check failed', 'jw-core'));
}
$title = isset($_POST['title']) ? sanitize_text_field(wp_unslash($_POST['title'])) : '';
$body = isset($_POST['body']) ? wp_kses_post(wp_unslash($_POST['body'])) : '';
$image = isset($_POST['image']) ? esc_url_raw(wp_unslash($_POST['image'])) : '';
if (!empty($title) && !empty($body)) {
if ($id > 0) {
$wpdb->update(
$table_name,
['title' => $title, 'body' => $body, 'image' => $image],
['id' => $id],
['%s', '%s', '%s'],
['%d']
);
$message = esc_html__('Notification updated successfully.', 'jw-core');
} else {
$wpdb->insert(
$table_name,
['title' => $title, 'body' => $body, 'image' => $image],
['%s', '%s', '%s']
);
$message = esc_html__('Notification created successfully.', 'jw-core');
$id = $wpdb->insert_id;
}
echo '<div class="notice notice-success is-dismissible"><p>' . esc_html($message) . '</p></div>';
} else {
echo '<div class="notice notice-error is-dismissible"><p>' . esc_html__('Title and Body are required.', 'jw-core') . '</p></div>';
}
}
// Render Add/Edit Form
if ($action === 'add' || $action === 'edit') {
$item = ['title' => '', 'body' => '', 'image' => ''];
if ($action === 'edit' && $id > 0) {
$item = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE id = %d", $id), ARRAY_A);
if (!$item) {
wp_die(esc_html__('Notification not found.', 'jw-core'));
}
}
?>
<div class="wrap">
<h1 class="wp-heading-inline">
<?php echo $action === 'edit' ? esc_html__('Edit Notification', 'jw-core') : esc_html__('Add New Notification', 'jw-core'); ?>
</h1>
<a href="?page=jw-notifications" class="page-title-action"><?php esc_html_e('Back to List', 'jw-core'); ?></a>
<form method="post" action="">
<?php wp_nonce_field('jw_notification_save_nonce'); ?>
<table class="form-table">
<tr>
<th scope="row"><label for="title"><?php esc_html_e('Title', 'jw-core'); ?> <span class="description"><?php esc_html_e('(required)', 'jw-core'); ?></span></label></th>
<td><input name="title" type="text" id="title" value="<?php echo esc_attr($item['title']); ?>" class="regular-text" required></td>
</tr>
<tr>
<th scope="row"><label for="body"><?php esc_html_e('Body', 'jw-core'); ?> <span class="description"><?php esc_html_e('(required)', 'jw-core'); ?></span></label></th>
<td>
<?php
wp_editor($item['body'], 'body', [
'textarea_name' => 'body',
'media_buttons' => false,
'textarea_rows' => 10,
]);
?>
</td>
</tr>
<tr>
<th scope="row"><label for="image"><?php esc_html_e('Image', 'jw-core'); ?></label></th>
<td>
<input name="image" type="text" id="image" value="<?php echo esc_url($item['image']); ?>" class="regular-text">
<button type="button" class="button" id="jw_notification_upload_image"><?php esc_html_e('Select Image', 'jw-core'); ?></button>
<br>
<img id="jw_notification_image_preview" src="<?php echo esc_url($item['image']); ?>" style="max-width: 200px; margin-top: 10px; display: <?php echo empty($item['image']) ? 'none' : 'block'; ?>;" />
</td>
</tr>
</table>
<p class="submit">
<input type="submit" name="jw_notification_save" id="submit" class="button button-primary" value="<?php esc_attr_e('Save Notification', 'jw-core'); ?>">
</p>
</form>
</div>
<script>
jQuery(document).ready(function($){
var mediaUploader;
$('#jw_notification_upload_image').click(function(e) {
e.preventDefault();
if (mediaUploader) {
mediaUploader.open();
return;
}
mediaUploader = wp.media.frames.file_frame = wp.media({
title: '<?php echo esc_js(__('Select Notification Image', 'jw-core')); ?>',
button: {
text: '<?php echo esc_js(__('Select Image', 'jw-core')); ?>'
},
multiple: false
});
mediaUploader.on('select', function() {
var attachment = mediaUploader.state().get('selection').first().toJSON();
$('#image').val(attachment.url);
$('#jw_notification_image_preview').attr('src', attachment.url).show();
});
mediaUploader.open();
});
});
</script>
<?php
} else {
// Render List Table
global $jw_notification_list_table;
if (!isset($jw_notification_list_table)) {
require_once plugin_dir_path(__FILE__) . 'class-jw-notification-list-table.php';
$jw_notification_list_table = new JW_Notification_List_Table();
}
$jw_notification_list_table->prepare_items();
?>
<div class="wrap">
<h1 class="wp-heading-inline"><?php esc_html_e('Notifications', 'jw-core'); ?></h1>
<a href="?page=jw-notifications&action=add" class="page-title-action"><?php esc_html_e('Add New', 'jw-core'); ?></a>
<hr class="wp-header-end">
<form id="notifications-filter" method="post">
<input type="hidden" name="page" value="<?php echo esc_attr($_REQUEST['page']); ?>" />
<?php
$jw_notification_list_table->display();
?>
</form>
</div>
<?php
}