ru-se.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

Materialis_Logo_Nav_Menu.php (2980B)


      1 <?php
      2 global $wp_version;
      3 
      4 if (version_compare($wp_version, '5.3') >= 0) {
      5 
      6 	class Materialis_Logo_Nav_Menu extends Materialis_Logo_Nav_Menu_Base
      7 	{
      8 
      9 		public function walk($elements, $max_depth, ...$args)
     10 		{
     11 			return call_user_func_array(array($this,'_walk'),func_get_args());
     12 		}
     13 	}
     14 } else
     15 {
     16 	class Materialis_Logo_Nav_Menu extends Materialis_Logo_Nav_Menu_Base
     17 	{
     18 
     19 		public function walk($elements, $max_depth)
     20 		{
     21 			return call_user_func_array(array($this,'_walk'),func_get_args());
     22 		}
     23 	}
     24 }
     25 
     26 class Materialis_Logo_Nav_Menu_Base extends \Walker_Nav_Menu {
     27 
     28 
     29 	public function display_logo( &$output ) {
     30 		ob_start();
     31 		materialis_print_logo();
     32 		$logo = ob_get_clean();
     33 
     34 		$output .= "<li class='logo'>{$logo}</li>";
     35 	}
     36 
     37 	public function _walk( $elements, $max_depth ) {
     38 
     39 
     40 		$args   = array_slice( func_get_args(), 2 );
     41 		$output = '';
     42 
     43 		//invalid parameter or nothing to walk
     44 		if ( $max_depth < - 1 || empty( $elements ) ) {
     45 			return $output;
     46 		}
     47 
     48 		$parent_field = $this->db_fields['parent'];
     49 
     50 		// flat display
     51 		if ( - 1 == $max_depth ) {
     52 			$empty_array = array();
     53 			foreach ( $elements as $e ) {
     54 				$this->display_element( $e, $empty_array, 1, 0, $args, $output );
     55 			}
     56 
     57 			return $output;
     58 		}
     59 
     60 		/*
     61 		 * Need to display in hierarchical order.
     62 		 * Separate elements into two buckets: top level and children elements.
     63 		 * Children_elements is two dimensional array, eg.
     64 		 * Children_elements[10][] contains all sub-elements whose parent is 10.
     65 		 */
     66 		$top_level_elements = array();
     67 		$children_elements  = array();
     68 		foreach ( $elements as $e ) {
     69 			if ( empty( $e->$parent_field ) ) {
     70 				$top_level_elements[] = $e;
     71 			} else {
     72 				$children_elements[ $e->$parent_field ][] = $e;
     73 			}
     74 		}
     75 
     76 		/*
     77 		 * When none of the elements is top level.
     78 		 * Assume the first one must be root of the sub elements.
     79 		 */
     80 		if ( empty( $top_level_elements ) ) {
     81 
     82 			$first = array_slice( $elements, 0, 1 );
     83 			$root  = $first[0];
     84 
     85 			$top_level_elements = array();
     86 			$children_elements  = array();
     87 			foreach ( $elements as $e ) {
     88 				if ( $root->$parent_field == $e->$parent_field ) {
     89 					$top_level_elements[] = $e;
     90 				} else {
     91 					$children_elements[ $e->$parent_field ][] = $e;
     92 				}
     93 			}
     94 		}
     95 
     96 
     97 		$display_logo_after = ( count( $top_level_elements ) / 2 );
     98 
     99 		foreach ( $top_level_elements as $index => $e ) {
    100 
    101 			if ( $index + 1 > $display_logo_after && $index <= $display_logo_after ) {
    102 				$this->display_logo( $output );
    103 			}
    104 
    105 			$this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
    106 		}
    107 
    108 		/*
    109 		 * If we are displaying all levels, and remaining children_elements is not empty,
    110 		 * then we got orphans, which should be displayed regardless.
    111 		 */
    112 		if ( ( $max_depth == 0 ) && count( $children_elements ) > 0 ) {
    113 			$empty_array = array();
    114 			foreach ( $children_elements as $orphans ) {
    115 				foreach ( $orphans as $op ) {
    116 					$this->display_element( $op, $empty_array, 1, 0, $args, $output );
    117 				}
    118 			}
    119 		}
    120 
    121 		return $output;
    122 	}
    123 
    124 }