功能演示
代码
function zib_register_keywords_page() {
add_menu_page(
'热门搜索关键词',
'热门关键词管理',
'manage_options',
'zib_keywords',
'zib_keywords_page',
'dashicons-search',
2
);
}
add_action('admin_menu', 'zib_register_keywords_page');
function zib_keywords_page() {
$filter_types = isset($_GET['filter_types']) ? $_GET['filter_types'] : [];
if (isset($_POST['submit_add_keyword'])) {
$new_keyword = sanitize_text_field($_POST['new_keyword']);
$search_count = intval($_POST['search_count']);
$keyword_type = sanitize_text_field($_POST['keyword_type']);
if (!empty($new_keyword)) {
$keywords = zib_get_option('search_keywords');
if (!is_array($keywords)) {
$keywords = [];
}
$keywords[$new_keyword . '&type=' . $keyword_type] = $search_count;
arsort($keywords);
zib_update_option('search_keywords', $keywords);
}
}
if (isset($_POST['submit_edit_keyword'])) {
$old_key = sanitize_text_field($_POST['old_key']);
$new_keyword = sanitize_text_field($_POST['edit_keyword']);
$new_count = intval($_POST['edit_count']);
$new_type = sanitize_text_field($_POST['edit_type']);
if (!empty($old_key) && !empty($new_keyword)) {
$keywords = zib_get_option('search_keywords');
if (is_array($keywords) && isset($keywords[$old_key])) {
unset($keywords[$old_key]);
$keywords[$new_keyword . '&type=' . $new_type] = $new_count;
arsort($keywords);
zib_update_option('search_keywords', $keywords);
}
}
}
if (isset($_POST['bulk_action']) && $_POST['bulk_action'] == 'delete' && isset($_POST['selected_keywords'])) {
$selected_keywords = $_POST['selected_keywords'];
$keywords = zib_get_option('search_keywords');
if (is_array($keywords)) {
foreach ($selected_keywords as $keyword) {
unset($keywords[$keyword]);
}
zib_update_option('search_keywords', $keywords);
}
}
$keywords = zib_get_option('search_keywords');
if (!empty($keywords)) {
arsort($keywords);
}
if (!empty($filter_types)) {
$filtered_keywords = [];
foreach ($keywords as $key => $value) {
$keyword_type = 'unknown';
if (preg_match('/&type=(post|user|forum|plate)/', $key, $matches)) {
$keyword_type = $matches[1];
}
if (!in_array($keyword_type, $filter_types) && $keyword_type !== 'unknown') {
continue;
}
$filtered_keywords[$key] = $value;
}
$keywords = $filtered_keywords;
}
?>
<div class="wrap">
<h1>热门搜索关键词</h1>
<div>
<button id="add_new_keyword" class="button">添加热词</button>
<button id="bulk_delete" class="button">批量删除</button><br>
<form method="get" action="<?php echo admin_url('admin.php'); ?>" style="display: inline-block;">
<input type="hidden" name="page" value="zib_keywords" />
<div style="display: flex; gap: 10px; align-items: center;">
<label style="margin-right: 10px;">筛选类型:</label>
<label style="margin-right: 10px;">
<input type="checkbox" name="filter_types[]" value="post" <?php echo in_array('post', $filter_types) ? 'checked' : ''; ?>>
文章
</label>
<label style="margin-right: 10px;">
<input type="checkbox" name="filter_types[]" value="user" <?php echo in_array('user', $filter_types) ? 'checked' : ''; ?>>
用户
</label>
<label style="margin-right: 10px;">
<input type="checkbox" name="filter_types[]" value="forum" <?php echo in_array('forum', $filter_types) ? 'checked' : ''; ?>>
论坛
</label>
<label style="margin-right: 10px;">
<input type="checkbox" name="filter_types[]" value="plate" <?php echo in_array('plate', $filter_types) ? 'checked' : ''; ?>>
板块
</label>
<input type="submit" value="筛选" class="button" />
</div>
</form>
</div>
<form method="post" action="" id="keywords_form">
<table class="wp-list-table widefat fixed striped">
<thead>
<tr>
<th><input type="checkbox" id="select_all" /></th>
<th>#</th>
<th>关键词</th>
<th>搜索次数</th>
<th>搜索类型</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php
if (!empty($keywords)) {
$i = 1;
foreach ($keywords as $key => $value) {
$keyword_type = 'unknown';
if (preg_match('/&type=(post|user|forum|plate)/', $key, $matches)) {
$keyword_type = $matches[1];
}
$keyword_base = strtok($key, '&');
$type = '其他'; // 默认值
switch ($keyword_type) {
case 'post':
$type = '文章';
break;
case 'user':
$type = '用户';
break;
case 'forum':
$type = '论坛';
break;
case 'plate':
$type = '板块';
break;
case 'other':
$type = '其他';
break;
}
echo "<tr>
<td><input type='checkbox' name='selected_keywords[]' value='$key' /></td> <!-- 复选框 -->
<td>$i</td> <!-- 序号 -->
<td>$keyword_base</td>
<td>$value</td>
<td>$type</td>
<td>
<button type='button' class='button edit_keyword'
data-keyword='$keyword_base'
data-count='$value'
data-type='$keyword_type'
data-fullkey='$key'>编辑</button>
<button class='button delete_keyword' data-keyword='$key'>删除</button>
</td>
</tr>";
$i++;
}
}
?>
</tbody>
</table>
</form>
</div>
<div id="add_keyword_modal" style="display: none;" class="modal">
<div class="modal-content">
<span id="close_modal" class="close">×</span>
<h2>添加新热词</h2>
<form method="post" action="">
<p>
<label for="new_keyword">关键词:</label>
<input type="text" name="new_keyword" required />
</p>
<p>
<label for="search_count">搜索次数:</label>
<input type="number" name="search_count" required />
</p>
<p>
<label for="keyword_type">搜索类型:</label>
<select name="keyword_type" required>
<option value="post">文章</option>
<option value="user">用户</option>
<option value="forum">论坛</option>
<option value="plate">板块</option>
</select>
</p>
<input type="submit" name="submit_add_keyword" value="添加" class="button" />
</form>
</div>
</div>
<div id="edit_keyword_modal" class="modal">
<div class="modal-content">
<span class="close" id="close_edit_modal">×</span>
<h2>修改热词</h2>
<form method="post" action="">
<input type="hidden" name="old_key" id="edit_old_key">
<p>
<label for="edit_keyword">关键词:</label>
<input type="text" name="edit_keyword" id="edit_keyword" required />
</p>
<p>
<label for="edit_count">搜索次数:</label>
<input type="number" name="edit_count" id="edit_count" required />
</p>
<p>
<label for="edit_type">搜索类型:</label>
<select name="edit_type" id="edit_type" required>
<option value="post">文章</option>
<option value="user">用户</option>
<option value="forum">论坛</option>
<option value="plate">板块</option>
</select>
</p>
<input type="submit" name="submit_edit_keyword" value="修改" class="button button-primary" />
</form>
</div>
</div>
<style>
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.4);
padding-top: 60px;
}
.modal-content {
background-color: #fefefe;
margin: 5% auto;
padding: 20px;
border: 1px solid #888;
width: 300px;
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
</style>
<script>
var modal = document.getElementById("add_keyword_modal");
var btn = document.getElementById("add_new_keyword");
var span = document.getElementById("close_modal");
btn.onclick = function() {
modal.style.display = "block";
}
span.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
document.getElementById("select_all").onclick = function() {
var checkboxes = document.querySelectorAll("input[name='selected_keywords[]']");
for (var checkbox of checkboxes) {
checkbox.checked = this.checked;
}
}
const deleteButtons = document.querySelectorAll('.delete_keyword');
deleteButtons.forEach(function(button) {
button.addEventListener('click', function() {
const keyword = button.dataset.keyword;
if (confirm('确定删除此关键词吗?')) {
const formData = new FormData();
formData.append('action', 'delete_keyword');
formData.append('keyword', keyword);
fetch('<?php echo admin_url('admin-ajax.php'); ?>', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert('关键词删除成功');
location.reload();
} else {
alert('删除失败,请重试');
}
});
}
});
});
document.addEventListener('DOMContentLoaded', function() {
const editModal = document.getElementById("edit_keyword_modal");
const closeEditBtn = document.getElementById("close_edit_modal");
const editButtons = document.querySelectorAll('.edit_keyword');
closeEditBtn.onclick = function() {
editModal.style.display = "none";
}
editButtons.forEach(function(button) {
button.addEventListener('click', function(e) {
e.preventDefault();
const keyword = this.dataset.keyword;
const count = this.dataset.count;
const type = this.dataset.type;
const fullkey = this.dataset.fullkey;
document.getElementById('edit_old_key').value = fullkey;
document.getElementById('edit_keyword').value = keyword;
document.getElementById('edit_count').value = count;
document.getElementById('edit_type').value = type;
editModal.style.display = "block";
});
});
window.onclick = function(event) {
if (event.target == editModal) {
editModal.style.display = "none";
}
if (event.target == modal) {
modal.style.display = "none";
}
}
});
document.getElementById('bulk_delete').addEventListener('click', function(e) {
e.preventDefault();
const selectedBoxes = document.querySelectorAll("input[name='selected_keywords[]']:checked");
if (selectedBoxes.length === 0) {
alert('请至少选择一个关键词');
return;
}
if (confirm('确定要删除选中的 ' + selectedBoxes.length + ' 个关键词吗?')) {
const formData = new FormData();
formData.append('action', 'bulk_delete_keywords');
selectedBoxes.forEach(box => {
formData.append('keywords[]', box.value);
});
fetch('<?php echo admin_url('admin-ajax.php'); ?>', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert('成功删除 ' + selectedBoxes.length + ' 个关键词');
location.reload();
} else {
alert('删除失败,请重试');
}
});
}
});
</script>
<?php
}
add_action('wp_ajax_delete_keyword', 'zib_delete_keyword');
function zib_delete_keyword() {
if (!isset($_POST['keyword']) || !current_user_can('manage_options')) {
wp_send_json_error(['message' => '权限不足或缺少关键词']);
}
$keyword = sanitize_text_field($_POST['keyword']);
$keywords = zib_get_option('search_keywords');
if (is_array($keywords) && isset($keywords[$keyword])) {
unset($keywords[$keyword]);
zib_update_option('search_keywords', $keywords);
wp_send_json_success();
}
wp_send_json_error(['message' => '关键词不存在']);
}
add_action('wp_ajax_bulk_delete_keywords', 'zib_bulk_delete_keywords');
function zib_bulk_delete_keywords() {
if (!current_user_can('manage_options')) {
wp_send_json_error(['message' => '权限不足']);
}
if (!isset($_POST['keywords']) || !is_array($_POST['keywords'])) {
wp_send_json_error(['message' => '未选择关键词']);
}
$keywords = zib_get_option('search_keywords');
if (!is_array($keywords)) {
wp_send_json_error(['message' => '关键词列表无效']);
}
foreach ($_POST['keywords'] as $keyword) {
$keyword = sanitize_text_field($keyword);
if (isset($keywords[$keyword])) {
unset($keywords[$keyword]);
}
}
zib_update_option('search_keywords', $keywords);
wp_send_json_success();
}
© 版权声明
本站资源仅供学习交流使用请勿商业运营,严禁从事违法,侵权等任何非法活动,否则后果自负!
THE END
暂无评论内容