Advertisement
artemsemkin

Untitled

May 26th, 2023
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.18 KB | None | 0 0
  1. <?php
  2.  
  3. class Arts_Walker_Nav_Menu_Overlay extends Walker_Nav_Menu {
  4.   /**
  5.    * What the class handles.
  6.    *
  7.    * @since 3.0.0
  8.    * @var string
  9.    *
  10.    * @see Walker::$tree_type
  11.    */
  12.   public $tree_type = array( 'post_type', 'taxonomy', 'custom' );
  13.  
  14.   /**
  15.    * Database fields to use.
  16.    *
  17.    * @since 3.0.0
  18.    * @todo Decouple this.
  19.    * @var array
  20.    *
  21.    * @see Walker::$db_fields
  22.    */
  23.   public $db_fields = array(
  24.     'parent' => 'menu_item_parent',
  25.     'id'     => 'db_id',
  26.   );
  27.  
  28.   /**
  29.    * Starts the list before the elements are added.
  30.    *
  31.    * @since 3.0.0
  32.    *
  33.    * @see Walker::start_lvl()
  34.    *
  35.    * @param string   $output Used to append additional content (passed by reference).
  36.    * @param int      $depth  Depth of menu item. Used for padding.
  37.    * @param stdClass $args   An object of wp_nav_menu() arguments.
  38.    */
  39.   public function start_lvl( &$output, $depth = 0, $args = null ) {
  40.     if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
  41.       $t = '';
  42.       $n = '';
  43.     } else {
  44.       $t = "\t";
  45.       $n = "\n";
  46.     }
  47.     $indent = str_repeat( $t, $depth );
  48.  
  49.     // Default class.
  50.     $classes = array( 'sub-menu' );
  51.  
  52.     /**
  53.      * Filters the CSS class(es) applied to a menu list element.
  54.      *
  55.      * @since 4.8.0
  56.      *
  57.      * @param string[] $classes Array of the CSS classes that are applied to the menu `<ul>` element.
  58.      * @param stdClass $args    An object of `wp_nav_menu()` arguments.
  59.      * @param int      $depth   Depth of menu item. Used for padding.
  60.      */
  61.     $class_names = join( ' ', apply_filters( 'nav_menu_submenu_css_class', $classes, $args, $depth ) );
  62.     $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
  63.  
  64.     $output .= "{$n}{$indent}<ul$class_names>{$n}";
  65.   }
  66.  
  67.   /**
  68.    * Ends the list of after the elements are added.
  69.    *
  70.    * @since 3.0.0
  71.    *
  72.    * @see Walker::end_lvl()
  73.    *
  74.    * @param string   $output Used to append additional content (passed by reference).
  75.    * @param int      $depth  Depth of menu item. Used for padding.
  76.    * @param stdClass $args   An object of wp_nav_menu() arguments.
  77.    */
  78.   public function end_lvl( &$output, $depth = 0, $args = null ) {
  79.     if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
  80.       $t = '';
  81.       $n = '';
  82.     } else {
  83.       $t = "\t";
  84.       $n = "\n";
  85.     }
  86.     $indent  = str_repeat( $t, $depth );
  87.     $output .= "$indent</ul>{$n}";
  88.   }
  89.  
  90.   /**
  91.    * Starts the element output.
  92.    *
  93.    * @since 3.0.0
  94.    * @since 4.4.0 The {@see 'nav_menu_item_args'} filter was added.
  95.    *
  96.    * @see Walker::start_el()
  97.    *
  98.    * @param string   $output Used to append additional content (passed by reference).
  99.    * @param WP_Post  $item   Menu item data object.
  100.    * @param int      $depth  Depth of menu item. Used for padding.
  101.    * @param stdClass $args   An object of wp_nav_menu() arguments.
  102.    * @param int      $id     Current item ID.
  103.    */
  104.   public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
  105.     if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
  106.       $t = '';
  107.       $n = '';
  108.     } else {
  109.       $t = "\t";
  110.       $n = "\n";
  111.     }
  112.     $indent = ( $depth ) ? str_repeat( $t, $depth ) : '';
  113.  
  114.     $classes   = empty( $item->classes ) ? array() : (array) $item->classes;
  115.     $classes[] = 'menu-item-' . $item->ID;
  116.  
  117.     /**
  118.      * Filters the arguments for a single nav menu item.
  119.      *
  120.      * @since 4.4.0
  121.      *
  122.      * @param stdClass $args  An object of wp_nav_menu() arguments.
  123.      * @param WP_Post  $item  Menu item data object.
  124.      * @param int      $depth Depth of menu item. Used for padding.
  125.      */
  126.     $args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
  127.  
  128.     /**
  129.      * Filters the CSS classes applied to a menu item's list item element.
  130.      *
  131.      * @since 3.0.0
  132.      * @since 4.1.0 The `$depth` parameter was added.
  133.      *
  134.      * @param string[] $classes Array of the CSS classes that are applied to the menu item's `<li>` element.
  135.      * @param WP_Post  $item    The current menu item.
  136.      * @param stdClass $args    An object of wp_nav_menu() arguments.
  137.      * @param int      $depth   Depth of menu item. Used for padding.
  138.      */
  139.     $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
  140.     $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
  141.  
  142.     /**
  143.      * Filters the ID applied to a menu item's list item element.
  144.      *
  145.      * @since 3.0.1
  146.      * @since 4.1.0 The `$depth` parameter was added.
  147.      *
  148.      * @param string   $menu_id The ID that is applied to the menu item's `<li>` element.
  149.      * @param WP_Post  $item    The current menu item.
  150.      * @param stdClass $args    An object of wp_nav_menu() arguments.
  151.      * @param int      $depth   Depth of menu item. Used for padding.
  152.      */
  153.     $id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args, $depth );
  154.     $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
  155.  
  156.     $output .= $indent . '<li' . $id . $class_names . '>';
  157.  
  158.     $atts           = array();
  159.     $atts['title']  = ! empty( $item->attr_title ) ? $item->attr_title : '';
  160.     $atts['target'] = ! empty( $item->target ) ? $item->target : '';
  161.     if ( '_blank' === $item->target && empty( $item->xfn ) ) {
  162.       $atts['rel'] = 'noopener noreferrer';
  163.     } else {
  164.       $atts['rel'] = $item->xfn;
  165.     }
  166.     $atts['href']                   = ! empty( $item->url ) ? $item->url : '';
  167.         $atts['data-pjax-link'] = 'overlayMenu';
  168.  
  169.     if ( $depth === 0 ) {
  170.       $atts['class'] = get_theme_mod( 'menu_overlay_top_heading_preset', 'h2' );
  171.     } else {
  172.       $atts['class'] = get_theme_mod( 'menu_overlay_sub_heading_preset', 'h3' );
  173.     }
  174.         $atts['aria-current'] = $item->current ? 'page' : '';
  175.  
  176.     /**
  177.      * Filters the HTML attributes applied to a menu item's anchor element.
  178.      *
  179.      * @since 3.6.0
  180.      * @since 4.1.0 The `$depth` parameter was added.
  181.      *
  182.      * @param array $atts {
  183.      *     The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
  184.      *
  185.      *     @type string $title        Title attribute.
  186.      *     @type string $target       Target attribute.
  187.      *     @type string $rel          The rel attribute.
  188.      *     @type string $href         The href attribute.
  189.      *     @type string $aria_current The aria-current attribute.
  190.      * }
  191.      * @param WP_Post  $item  The current menu item.
  192.      * @param stdClass $args  An object of wp_nav_menu() arguments.
  193.      * @param int      $depth Depth of menu item. Used for padding.
  194.      */
  195.     $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
  196.  
  197.     $attributes = '';
  198.     foreach ( $atts as $attr => $value ) {
  199.       if ( is_scalar( $value ) && '' !== $value && false !== $value ) {
  200.         $value       = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  201.         $attributes .= ' ' . $attr . '="' . $value . '"';
  202.       }
  203.     }
  204.  
  205.     /** This filter is documented in wp-includes/post-template.php */
  206.     $title = apply_filters( 'the_title', $item->title, $item->ID );
  207.  
  208.     /**
  209.      * Filters a menu item's title.
  210.      *
  211.      * @since 4.4.0
  212.      *
  213.      * @param string   $title The menu item's title.
  214.      * @param WP_Post  $item  The current menu item.
  215.      * @param stdClass $args  An object of wp_nav_menu() arguments.
  216.      * @param int      $depth Depth of menu item. Used for padding.
  217.      */
  218.     $title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
  219.  
  220.     $item_output  = $args->before;
  221.     $item_output .= '<a' . $attributes . '>';
  222.     $item_output .= $args->link_before . $title . $args->link_after;
  223.     $item_output .= '</a>';
  224.     $item_output .= $args->after;
  225.  
  226.     /**
  227.      * Filters a menu item's starting output.
  228.      *
  229.      * The menu item's starting output only includes `$args->before`, the opening `<a>`,
  230.      * the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is
  231.      * no filter for modifying the opening and closing `<li>` for a menu item.
  232.      *
  233.      * @since 3.0.0
  234.      *
  235.      * @param string   $item_output The menu item's starting HTML output.
  236.      * @param WP_Post  $item        Menu item data object.
  237.      * @param int      $depth       Depth of menu item. Used for padding.
  238.      * @param stdClass $args        An object of wp_nav_menu() arguments.
  239.      */
  240.     $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  241.   }
  242.  
  243.   /**
  244.    * Ends the element output, if needed.
  245.    *
  246.    * @since 3.0.0
  247.    *
  248.    * @see Walker::end_el()
  249.    *
  250.    * @param string   $output Used to append additional content (passed by reference).
  251.    * @param WP_Post  $item   Page data object. Not used.
  252.    * @param int      $depth  Depth of page. Not Used.
  253.    * @param stdClass $args   An object of wp_nav_menu() arguments.
  254.    */
  255.   public function end_el( &$output, $item, $depth = 0, $args = null ) {
  256.     if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
  257.       $t = '';
  258.       $n = '';
  259.     } else {
  260.       $t = "\t";
  261.       $n = "\n";
  262.     }
  263.     $output .= "</li>{$n}";
  264.   }
  265.  
  266. }
  267.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement