Files
CyyWordpress/inc/bulmapress_navwalker.php
2026-04-29 19:04:20 +08:00

103 lines
3.5 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* Bulma Navwalker secure, PHP 8.1 compatible rewrite
*
* @package CyyWordpress
*/
if ( ! class_exists( 'Bulmapress_Navwalker' ) ) :
/**
* Custom Bulma nav-walker for WordPress menus.
*/
class Bulmapress_Navwalker extends Walker_Nav_Menu {
/**
* Opens a dropdown container.
*
* @param string $output Used to append additional content (passed by reference).
* @param int $depth Depth of menu item.
* @param stdClass|null $args An object of wp_nav_menu() arguments.
*/
public function start_lvl( string &$output, int $depth = 0, ?stdClass $args = null ): void {
$output .= '<div class="navbar-dropdown">';
}
/**
* Opens a menu item element and its link.
*
* @param string $output Used to append additional content (passed by reference).
* @param WP_Post $item Menu item data object.
* @param int $depth Depth of menu item.
* @param stdClass|null $args An object of wp_nav_menu() arguments.
* @param int $id Current item ID.
*/
public function start_el( string &$output, \WP_Post $item, int $depth = 0, ?stdClass $args = null, int $id = 0 ): void {
$has_children = isset( $args->walker ) && $args->walker->has_children;
if ( $has_children ) {
// Mark the item so end_el knows it needs a </div> instead of </a>.
$item->classes[] = 'has_children';
$output .= '<div class="navbar-item has-dropdown is-hoverable">';
$output .= '<a class="navbar-link" href="' . esc_url( $item->url ) . '">'
. esc_html( $item->title )
. '</a>';
} else {
$output .= '<a class="navbar-item" href="' . esc_url( $item->url ) . '">'
. esc_html( $item->title );
}
}
/**
* Closes a menu item element.
*
* @param string $output Used to append additional content (passed by reference).
* @param WP_Post $item Menu item data object.
* @param int $depth Depth of menu item.
* @param stdClass|null $args An object of wp_nav_menu() arguments.
*/
public function end_el( string &$output, \WP_Post $item, int $depth = 0, ?stdClass $args = null ): void {
if ( in_array( 'has_children', (array) $item->classes, true ) ) {
// Dropdown wrapper opened with <div>; close the div (inner <a> was already closed).
$output .= '</div>';
} else {
// Regular item opened with <a>; close it.
$output .= '</a>';
}
}
/**
* Closes a dropdown container.
*
* @param string $output Used to append additional content (passed by reference).
* @param int $depth Depth of menu item.
* @param stdClass|null $args An object of wp_nav_menu() arguments.
*/
public function end_lvl( string &$output, int $depth = 0, ?stdClass $args = null ): void {
$output .= '</div>'; // .navbar-dropdown
}
/**
* Fallback: output a link to the home page when no menu is assigned.
*
* @param array $args Arguments from wp_nav_menu().
*/
public static function fallback( array $args ): void {
if ( current_user_can( 'manage_options' ) ) {
echo '<a class="navbar-item" href="' . esc_url( admin_url( 'nav-menus.php' ) ) . '">'
. esc_html__( '请添加菜单', 'cyywordpress' )
. '</a>';
} else {
echo '<a class="navbar-item" href="' . esc_url( home_url( '/' ) ) . '">'
. esc_html__( '首页', 'cyywordpress' )
. '</a>';
}
}
}
// Back-compat alias so existing wp_nav_menu() calls still work.
class_alias( 'Bulmapress_Navwalker', 'bulmapress_navwalker' );
endif;