function momaiz_get_category_slug() { try { // Verify nonce if (!isset($_POST['security']) || !wp_verify_nonce($_POST['security'], 'momaiz_filter_nonce')) { throw new Exception(__('Invalid security nonce', 'afedny-filter')); } // Validate category ID if (!isset($_POST['cat_id']) || !is_numeric($_POST['cat_id'])) { throw new Exception(__('Invalid category ID', 'afedny-filter')); } $cat_id = absint($_POST['cat_id']); $category = get_term($cat_id, 'category'); if (is_wp_error($category) || !$category) { throw new Exception(__('Category not found', 'afedny-filter')); } // Get hierarchical path $path = []; $ancestors = get_ancestors($cat_id, 'category'); foreach (array_reverse($ancestors) as $ancestor_id) { $ancestor = get_term($ancestor_id, 'category'); if ($ancestor && !is_wp_error($ancestor)) { $path[] = $ancestor->slug; } } $path[] = $category->slug; $category_path = implode('/', $path); wp_send_json_success([ 'url' => esc_url_raw(home_url("/category/{$category_path}/")) ]); } catch (Exception $e) { wp_send_json_error([ 'message' => esc_html($e->getMessage()) ], 400); } } add_action('wp_ajax_momaiz_get_category_slug', 'momaiz_get_category_slug'); add_action('wp_ajax_nopriv_momaiz_get_category_slug', 'momaiz_get_category_slug');