[ 'background' => '#9e5a63', // фон для banned 'color' => '#ffffff', // белый цвет для рамки 'placeholder_color' => '#ffffff', // цвет placeholder для banned 'allow_new_tags' => false // запрещаем новые теги для banned ], 'keys' => [ 'background' => '#e8f2ff', // синий фон для keys 'color' => '#2271b1', // синий цвет для рамки 'placeholder_color' => '#2271b1', // цвет placeholder для keys 'allow_new_tags' => true // разрешаем новые теги для keys ], 'post_tag' => [ 'background' => '#31708e', // светлый фон для post_tag 'color' => '#ffffff', // серый цвет для рамки 'placeholder_color' => '#ffffff', // цвет placeholder для post_tag 'allow_new_tags' => false // запрещаем новые теги для post_tag ] ]; ?> $taxonomy, 'hide_empty' => false, 'number' => 100, ]; // Если есть поисковый запрос, добавляем поиск и сортировку по релевантности if (!empty($search)) { $args['search'] = $search; $args['orderby'] = 'name'; $args['order'] = 'ASC'; } $terms = get_terms($args); $result = []; foreach ($terms as $term) { $result[] = [ 'id' => $term->term_id, 'text' => $term->name ]; } // Сортируем по релевантности, если есть поисковый запрос if (!empty($search)) { usort($result, function($a, $b) use ($search) { $a_text = $a['text']; $b_text = $b['text']; $search_lower = strtolower($search); // Приоритет: точное совпадение if (strtolower($a_text) === $search_lower && strtolower($b_text) !== $search_lower) { return -1; } if (strtolower($b_text) === $search_lower && strtolower($a_text) !== $search_lower) { return 1; } // Приоритет: начинается с поискового запроса $a_starts = stripos($a_text, $search) === 0; $b_starts = stripos($b_text, $search) === 0; if ($a_starts && !$b_starts) { return -1; } if ($b_starts && !$a_starts) { return 1; } // Приоритет: содержит поисковый запрос $a_contains = stripos($a_text, $search) !== false; $b_contains = stripos($b_text, $search) !== false; if ($a_contains && !$b_contains) { return -1; } if ($b_contains && !$a_contains) { return 1; } // Если одинаковый приоритет - сортируем по алфавиту return strcasecmp($a_text, $b_text); }); } wp_send_json($result); }); // AJAX обработчик для получения терминов поста add_action('wp_ajax_get_post_terms', function () { $taxonomy = sanitize_key($_POST['taxonomy']); $post_id = (int)$_POST['post_id']; $terms = wp_get_post_terms($post_id, $taxonomy); $result = []; foreach ($terms as $term) { $result[] = [ 'id' => $term->term_id, 'text' => $term->name ]; } wp_send_json($result); }); // AJAX обработчик для получения популярных тегов add_action('wp_ajax_get_popular_terms', function () { $taxonomy = sanitize_key($_GET['taxonomy']); // Получаем популярные теги (с наибольшим количеством постов) $terms = get_terms([ 'taxonomy' => $taxonomy, 'orderby' => 'count', 'order' => 'DESC', 'number' => 20, 'hide_empty' => false, ]); $result = []; foreach ($terms as $term) { $result[] = [ 'id' => $term->term_id, 'name' => $term->name, 'count' => $term->count // Добавляем количество использований ]; } wp_send_json($result); }); // Обработчик сохранения с исправленной логикой для новых тегов add_action('save_post', function($post_id) { // Проверяем права пользователя if (!current_user_can('edit_post', $post_id)) return; // Убираем автосохранение if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return; // Отладочная информация error_log('Save post hook called for post: ' . $post_id); error_log('POST data for keys: ' . ($_POST['keys_ids'] ?? 'not set')); error_log('POST data for banned: ' . ($_POST['banned_ids'] ?? 'not set')); error_log('POST data for post_tag: ' . ($_POST['post_tag_ids'] ?? 'not set')); $taxonomies = ['banned', 'keys', 'post_tag']; foreach ($taxonomies as $taxonomy) { if (isset($_POST[$taxonomy . '_ids'])) { $values = explode(',', $_POST[$taxonomy . '_ids']); $term_ids = []; foreach ($values as $value) { $value = trim($value); if (empty($value)) continue; // Если значение НЕ число, это новый тег (только для keys) if (!is_numeric($value) && $taxonomy === 'keys') { // Это название нового тега $term_name = $value; if (!empty($term_name)) { // Создаем новый терм $new_term = wp_insert_term($term_name, $taxonomy); if (!is_wp_error($new_term)) { $term_ids[] = $new_term['term_id']; error_log('Created new term: ' . $term_name . ' with ID: ' . $new_term['term_id']); } else if ($new_term->get_error_code() === 'term_exists') { // Если терм уже существует, получаем его ID $existing_term = get_term_by('name', $term_name, $taxonomy); if ($existing_term) { $term_ids[] = $existing_term->term_id; error_log('Term already exists: ' . $term_name . ' with ID: ' . $existing_term->term_id); } } else { error_log('Error creating term: ' . $term_name . ' - ' . $new_term->get_error_message()); } } } else { // Существующий ID термина $term_ids[] = intval($value); } } $term_ids = array_filter($term_ids); if (!empty($term_ids)) { wp_set_object_terms($post_id, $term_ids, $taxonomy, false); error_log('Set terms for ' . $taxonomy . ': ' . implode(', ', $term_ids)); } else { // Если нет терминов, очищаем wp_set_object_terms($post_id, [], $taxonomy, false); error_log('Cleared terms for ' . $taxonomy); } } } });