-
Notifications
You must be signed in to change notification settings - Fork 0
/
categories.php
237 lines (223 loc) · 8.12 KB
/
categories.php
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
// Function to handle the Categories admin page
function bt_manage_categories()
{
global $wpdb;
if ( current_user_can('manage_options') )
{
$action = !empty( $_REQUEST['action']) ? $_REQUEST['action'] : '';
$cat_id = !empty( $_REQUEST['cat_id']) ? $_REQUEST['cat_id'] : '';
if ( $action == 'add' )
{
$cat_name = !empty( $_REQUEST['cat_name']) ? $wpdb->escape(trim($_REQUEST['cat_name'])) : '';
$cat_link = !empty( $_REQUEST['cat_link']) ? $wpdb->escape(trim($_REQUEST['cat_link'])) : '';
// Validate
if ( mb_strlen($cat_name, 'UTF-8') == 0 || mb_strlen($cat_name, 'UTF-8') > 10 )
{
echo '<div class="error"><p><strong>錯誤:</strong>分類名稱需有至少1字,至多10字</p></div>';
$cat_name_ok = 0;
} else {
$cat_name_ok = 1;
}
if ( $cat_link != '' && preg_match('/^https?\:\/\//', $cat_link) && !preg_match('/^https?\:\/\/$/', $cat_link) )
{
$cat_link_ok = 1;
}
elseif ( empty($i[0]) && empty($i[1]) )
{
$cat_link_ok = 1;
} else {
$cat_link_ok = 0;
echo '<div class="error"><p><strong>錯誤:</strong>連結需為 http:// 或 https:// 開頭,或為空白</p></div>';
return false;
break;
}
// Operate the database if everything's alright
if ( $cat_name_ok == 1 && $cat_link_ok == 1)
{
$sql = "INSERT INTO " . WP_BTAEON_CATEGORIES_TABLE . " SET
category_name='$cat_name', category_link='$cat_link'";
$wpdb->get_results($sql);
$sql = "SELECT category_id FROM " . WP_BTAEON_CATEGORIES_TABLE . " WHERE
category_name='$cat_name' AND category_link='$cat_link' LIMIT 1";
$result = $wpdb->get_results($sql);
if ( empty($result) )
{
echo '<div class="error"><p>我找在資料庫中不到您剛剛送出的資料,資料庫可能出問題了</p></div>';
} else {
echo '<div class="updated"><p>分類新增成功!</p></div>';
}
} else {
// Preserve user entry
$user_entries->category_name = $cat_name;
$user_entries->category_link = $cat_link;
}
} elseif ( $action == 'edit_save' ) {
$cat_name = !empty( $_REQUEST['cat_name']) ? $wpdb->escape(trim($_REQUEST['cat_name'])) : '';
$cat_link = !empty( $_REQUEST['cat_link']) ? $wpdb->escape(trim($_REQUEST['cat_link'])) : '';
// Validate
if ( mb_strlen($cat_name, 'UTF-8') == 0 || mb_strlen($cat_name, 'UTF-8') > 10 )
{
echo '<div class="error"><p><strong>錯誤:</strong>分類名稱需有至少1字,至多10字</p></div>';
$cat_name_ok = 0;
} else {
$cat_name_ok = 1;
}
if ( $cat_link != '' && preg_match('/^https?\:\/\//', $cat_link) && !preg_match('/^https?\:\/\/$/', $cat_link) )
{
$cat_link_ok = 1;
}
elseif ( empty($i[0]) && empty($i[1]) )
{
$cat_link_ok = 1;
} else {
$cat_link_ok = 0;
echo '<div class="error"><p><strong>錯誤:</strong>連結需為 http:// 或 https:// 開頭,或為空白</p></div>';
return false;
break;
}
// Operate the database if everything's alright
if ( $cat_name_ok == 1 && $cat_link_ok == 1)
{
$sql = "UPDATE " . WP_BTAEON_CATEGORIES_TABLE . " SET
category_name='$cat_name', category_link='$cat_link' WHERE category_id='$cat_id'";
$wpdb->get_results($sql);
echo '<div class="updated"><p>' . $sql . '</p></div>';
$sql = "SELECT category_id FROM " . WP_BTAEON_CATEGORIES_TABLE . " WHERE
category_name='$cat_name' AND category_link='$cat_link' LIMIT 1";
echo '<div class="updated"><p>' . $cat_name . '</p></div>';
$result = $wpdb->get_results($sql);
if ( empty($result) )
{
echo '<div class="error"><p>我找在資料庫中不到您剛剛送出的資料,資料庫可能出問題了</p></div>';
} else {
echo '<div class="updated"><p>分類編輯成功!</p></div>';
}
} else {
// Preserve user entry
$user_entries->category_name = $cat_name;
$user_entries->category_link = $cat_link;
$error_with_saving = 1;
}
} elseif ( $action == 'delete' ) {
if ( empty($cat_id) )
{
echo '<div class="error"><p>您不能刪除不存在的公告</p></div>';
} else {
$sql = "DELETE FROM " . WP_BTAEON_CATEGORIES_TABLE . " WHERE category_id='$cat_id'";
$wpdb->query($sql);
$sql = "SELECT * FROM " . WP_BTAEON_CATEGORIES_TABLE . " WHERE category_id='$cat_id'";
$result = $wpdb->get_results($sql);
if ( empty($result) )
{
echo '<div class="updated"><p>分類刪除成功!</p></div>';
} else {
echo '<div class="error"><p><strong>錯誤:</strong>儘管已經發出刪除命令,該分類在資料庫中依然存在。</p></div>';
}
}
}
// Now follows a little bit of code that pulls in the main components of this page; the edit form and the list of categories
echo '<div class="wrap">';
if ( $action == 'edit' || ($action == 'edit_save' && $error_with_saving == 1))
{
echo '<h2>編輯分類</h2>';
if ( empty($cat_id) )
{
echo '<div class="error"><p>您並未指定分類 ID </p></div>';
} else {
bt_cats_edit_form('edit_save', $cat_id);
}
} else {
echo '<h2>新增分類</h2>';
bt_cats_edit_form();
echo '<h2>管理分類</h2>';
bt_cats_display();
}
echo '</div>';
} else {
echo '<div class="error"><p><strong>錯誤:</strong>您沒有權限檢視此頁</p></div>';
}
}
// The edit form
function bt_cats_edit_form( $mode = 'add', $cat_id = false )
{
global $wpdb, $user_entries;
$data = false;
if ( $cat_id != false )
{
if ( intval($cat_id) != $cat_id )
{
echo '<div class="error"><p>不乖!</p></div>';
return;
} else {
$data = $wpdb->get_results("SELECT * FROM " . WP_BTAEON_CATEGORIES_TABLE . " WHERE category_id='$cat_id' LIMIT 1");
if ( empty($data) )
{
echo '<div class="error"><p>找不到該分類</p></div>';
return;
}
$data = $data[0];
}
// Recover user entry
if ( !empty($user_entries) )
{
$data = $user_entries;
}
} else {
$data = $user_entries;
}
?>
<form name="catform" id="catform" class="wrap" method="post" action="<?php echo bloginfo('wpurl'); ?>/wp-admin/admin.php?page=btcat">
<input type="hidden" name="action" value="<?php echo $mode; ?>" />
<input type="hidden" name="cat_id" value="<?php echo $cat_id; ?>" />
<div id="linkadvanceddiv" class="postbox">
<div class="inside">
<p class="inside-text"><span>分類名稱</span>
<input type="text" name="cat_name" class="input" size="13" maxlength="10" value="<?php if ( !empty($data) ) echo $data->category_name; ?>" /></p>
<p class="inside-text"><span>分類連結</span>
<input type="text" name="cat_link" class="input" size="60" value="<?php if ( !empty($data) ) echo $data->category_link; ?>" /></p>
</div>
</div>
<input type="submit" name="save" class="button bold" value="儲存 »" />
</form>
<?php
}
// Show all categories
function bt_cats_display()
{
global $wpdb;
// Grab from the database
$sql = 'SELECT * FROM ' . WP_BTAEON_CATEGORIES_TABLE . ' ORDER BY category_id DESC';
$cats = $wpdb->get_results($sql);
if ( !empty($cats) )
{
?>
<table class="widefat page fixed" width="100%" cellpadding="3" cellspacing="3">
<thead>
<tr>
<th width="3%" class="manage-column" scope="col">ID</th>
<th class="manage-column" scope="col">分類名稱</th>
<th class="manage-column" scope="col">分類連結(「更多」)</th>
<th width="3%" class="manage-column" scope="col">編輯</th>
<th width="3%" class="manage-column" scope="col">刪除</th>
</tr>
</thead>
<?php
foreach ( $cats as $cat )
{
?>
<tr>
<th scope="row"><?php echo stripslashes($cat->category_id); ?></th>
<td><?php echo stripslashes($cat->category_name); ?></td>
<td><?php echo '<a href="', stripslashes($cat->category_link), '">', stripslashes($cat->category_link), '</a>'; ?></td>
<td><a href="<?php bloginfo('wpurl'); ?>/wp-admin/admin.php?page=btcat&action=edit&cat_id=<?php echo stripslashes($cat->category_id);?>" class='edit'>編輯</a></td>
<td><a href="<?php bloginfo('wpurl'); ?>/wp-admin/admin.php?page=btcat&action=delete&cat_id=<?php echo stripslashes($cat->category_id);?>" class="delete" onclick="return confirm(您確定要刪除此分類?)">刪除</a></td>
</tr>
<?php
}
echo '</table>';
} else {
echo '<p>資料庫中沒有分類</p>';
}
}
?>