add endpoints

This commit is contained in:
Profile Profile
2026-02-15 23:06:39 +03:00
parent 141d117b96
commit 485913ca23
4 changed files with 179 additions and 23 deletions

View File

@@ -1,5 +1,4 @@
<?php
// Убираем стандартный фильтр авторов для profile_article
add_action('admin_head-edit.php', 'cap_hide_default_author_filter');
function cap_hide_default_author_filter() {
@@ -71,6 +70,9 @@ function cap_filter_by_coauthor($post_type) {
// Обертка для фильтра и кнопки
echo '<div id="author-filter-wrapper">';
// Добавляем скрытое поле для сохранения post_type
echo '<input type="hidden" name="post_type" value="profile_article">';
echo '<select name="author_name" id="author_name_filter">';
echo '<option value="">Все авторы</option>';
@@ -107,22 +109,55 @@ function cap_filter_by_coauthor($post_type) {
}
// Модифицируем запрос для фильтрации
add_filter('parse_query', 'cap_filter_posts_by_coauthor');
add_filter('pre_get_posts', 'cap_filter_posts_by_coauthor', 999);
function cap_filter_posts_by_coauthor($query) {
global $pagenow, $typenow;
if (!is_admin() || $pagenow !== 'edit.php' || $typenow !== 'profile_article') {
// Проверяем, что это админка и главный запрос
if (!is_admin() || !$query->is_main_query()) {
return;
}
global $pagenow;
if ($pagenow !== 'edit.php') {
return;
}
// Получаем тип поста (может быть строкой или массивом)
$current_post_type = $query->get('post_type');
// Если массив - берем первый элемент
if (is_array($current_post_type)) {
$current_post_type = reset($current_post_type);
}
// Если пустой - берем из GET
if (empty($current_post_type) && isset($_GET['post_type'])) {
$current_post_type = is_array($_GET['post_type']) ? reset($_GET['post_type']) : $_GET['post_type'];
}
if ($current_post_type !== 'profile_article') {
return;
}
// Убираем пустой параметр s (поиск)
if (isset($_GET['s']) && (empty($_GET['s']) || $_GET['s'] === '')) {
$query->set('s', '');
unset($_GET['s']);
}
// Если выбран автор в фильтре
if (isset($_GET['author_name']) && !empty($_GET['author_name'])) {
$query->query_vars['tax_query'] = [
[
'taxonomy' => 'author',
'field' => 'slug',
'terms' => sanitize_text_field($_GET['author_name'])
]
$tax_query = $query->get('tax_query');
if (!is_array($tax_query)) {
$tax_query = [];
}
$tax_query[] = [
'taxonomy' => 'author',
'field' => 'slug',
'terms' => sanitize_text_field($_GET['author_name'])
];
$query->set('tax_query', $tax_query);
}
}
@@ -154,24 +189,50 @@ function cap_enqueue_select2($hook) {
width: "140px"
});
// Обработка очистки
// Получаем текущий URL для сброса
var baseUrl = "' . admin_url('edit.php?post_type=profile_article') . '";
// Обработка очистки - сохраняем post_type
$select.on("select2:clear", function(e) {
$(this).val("").trigger("change");
$(this).closest("form").submit();
window.location.href = baseUrl;
});
// Функция для удаления пустых полей формы
function removeEmptyFields($form) {
$form.find("input[type=text], input[type=search]").each(function() {
if ($(this).val() === "" || $(this).val() === null) {
$(this).remove();
}
});
}
// Обработка выбора
$select.on("select2:select", function(e) {
$(this).closest("form").submit();
var $form = $(this).closest("form");
removeEmptyFields($form);
$form.submit();
});
// Enter в поиске
$(document).on("keypress", ".select2-search__field", function(e) {
if (e.which === 13) {
e.preventDefault();
$select.closest("form").submit();
var $form = $select.closest("form");
removeEmptyFields($form);
$form.submit();
}
});
// Обработка клика по кнопке Фильтр
$("#cap-filter-submit").on("click", function(e) {
var $form = $(this).closest("form");
removeEmptyFields($form);
});
// Глобальная обработка отправки формы фильтров
$("#posts-filter").on("submit", function(e) {
removeEmptyFields($(this));
});
});
');