ru-se.com

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

Materialis_Logo_Page_Menu.php (2982B)


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