<commit_before><?php

class TimberMenu extends TimberCore
{

    public $MenuItemClass = 'TimberMenuItem';

    public $items = null;
    public $id = null;
    public $ID = null;
    public $name = null;
    public $term_id;

    /**
     * @param int $slug
     */
    function __construct($slug = 0) {
        $locations = get_nav_menu_locations();
        if ($slug != 0 && is_numeric($slug)) {
            $menu_id = $slug;
        } else if (is_array($locations) && count($locations)) {
            $menu_id = $this->get_menu_id_from_locations($slug, $locations);
        } else if ($slug === false) {
            $menu_id = false;
        } else {
            $menu_id = $this->get_menu_id_from_terms($slug);
        }
        if ($menu_id) {
            $this->init($menu_id);
        } else {
            TimberHelper::error_log("Sorry, the menu you were looking for wasn't found ('" . $slug . "'). Here's what Timber did find:");
        }
        return null;
    }

    /**
     * @param int $menu_id
     */
    private function init($menu_id) {
        $menu = wp_get_nav_menu_items($menu_id);
        $menu = self::order_children($menu);
        $this->items = $menu;
        $menu_info = wp_get_nav_menu_object($menu_id);
        $this->import($menu_info);
        $this->ID = $this->term_id;
        $this->id = $this->term_id;
    }

    /**
     * @param string $slug
     * @param array $locations
     * @return mixed
     */
    private function get_menu_id_from_locations($slug, $locations) {
        if ($slug === 0) {
            $slug = $this->get_menu_id_from_terms($slug);
        }
        if (is_numeric($slug)) {
            $slug = array_search($slug, $locations);
        }
        if (isset($locations[$slug])) {
            return $locations[$slug];
        }
        return null;
    }

    /**
     * @param int $slug
     * @return int
     */
    private function get_menu_id_from_terms($slug = 0) {
        if (!is_numeric($slug) && is_string($slug)) {
            //we have a string so lets search for that
            $menu_id = get_term_by('slug', $slug, 'nav_menu');
            if ($menu_id) {
                return $menu_id;
            }
            $menu_id = get_term_by('name', $slug, 'nav_menu');
            if ($menu_id) {
                return $menu_id;
            }
        }
        $menus = get_terms('nav_menu', array('hide_empty' => true));
        if (is_array($menus) && count($menus)) {
            if (isset($menus[0]->term_id)) {
                return $menus[0]->term_id;
            }
        }
        return 0;
    }

    /**
     * @param array $menu_items
     * @param int $parent_id
     * @return TimberMenuItem|null
     */
    function find_parent_item_in_menu($menu_items, $parent_id) {
        foreach ($menu_items as &$item) {
            if ($item->ID == $parent_id) {
                return $item;
            }
        }
        return null;
    }

    /**
     * @param array $items
     * @return array
     */
    function order_children($items) {
        $index = array();
        $menu = array();
        _wp_menu_item_classes_by_context($items);
        foreach ($items as $item) {
            if(isset($item->ID)){
                $index[$item->ID] = new $this->MenuItemClass($item);
            }
        }
        foreach ($index as $item) {
            if (isset($item->menu_item_parent) && $item->menu_item_parent && isset($index[$item->menu_item_parent])) {<commit_msg>check to make sure menu actually has items before ordering those items<commit_after><?php

class TimberMenu extends TimberCore
{

    public $MenuItemClass = 'TimberMenuItem';

    public $items = null;
    public $id = null;
    public $ID = null;
    public $name = null;
    public $term_id;

    /**
     * @param int $slug
     */
    function __construct($slug = 0) {
        $locations = get_nav_menu_locations();
        if ($slug != 0 && is_numeric($slug)) {
            $menu_id = $slug;
        } else if (is_array($locations) && count($locations)) {
            $menu_id = $this->get_menu_id_from_locations($slug, $locations);
        } else if ($slug === false) {
            $menu_id = false;
        } else {
            $menu_id = $this->get_menu_id_from_terms($slug);
        }
        if ($menu_id) {
            $this->init($menu_id);
        } else {
            TimberHelper::error_log("Sorry, the menu you were looking for wasn't found ('" . $slug . "'). Here's what Timber did find:");
        }
        return null;
    }

    /**
     * @param int $menu_id
     */
    private function init($menu_id) {
        $menu = wp_get_nav_menu_items($menu_id);
        if (is_array($menu)){
            $menu = self::order_children($menu);
        }
        $this->items = $menu;
        $menu_info = wp_get_nav_menu_object($menu_id);
        $this->import($menu_info);
        $this->ID = $this->term_id;
        $this->id = $this->term_id;
    }

    /**
     * @param string $slug
     * @param array $locations
     * @return mixed
     */
    private function get_menu_id_from_locations($slug, $locations) {
        if ($slug === 0) {
            $slug = $this->get_menu_id_from_terms($slug);
        }
        if (is_numeric($slug)) {
            $slug = array_search($slug, $locations);
        }
        if (isset($locations[$slug])) {
            return $locations[$slug];
        }
        return null;
    }

    /**
     * @param int $slug
     * @return int
     */
    private function get_menu_id_from_terms($slug = 0) {
        if (!is_numeric($slug) && is_string($slug)) {
            //we have a string so lets search for that
            $menu_id = get_term_by('slug', $slug, 'nav_menu');
            if ($menu_id) {
                return $menu_id;
            }
            $menu_id = get_term_by('name', $slug, 'nav_menu');
            if ($menu_id) {
                return $menu_id;
            }
        }
        $menus = get_terms('nav_menu', array('hide_empty' => true));
        if (is_array($menus) && count($menus)) {
            if (isset($menus[0]->term_id)) {
                return $menus[0]->term_id;
            }
        }
        return 0;
    }

    /**
     * @param array $menu_items
     * @param int $parent_id
     * @return TimberMenuItem|null
     */
    function find_parent_item_in_menu($menu_items, $parent_id) {
        foreach ($menu_items as &$item) {
            if ($item->ID == $parent_id) {
                return $item;
            }
        }
        return null;
    }

    /**
     * @param array $items
     * @return array
     */
    function order_children($items) {
        $index = array();
        $menu = array();
        foreach ($items as $item) {
            if(isset($item->ID)){
                $index[$item->ID] = new $this->MenuItemClass($item);
            }
        }
        foreach ($index as $item) {
            if (isset($item->menu_item_parent) && $item->menu_item_parent && isset($index[$item->menu_item_parent])) {
{'fraction_non_alphanumeric': 0.14654061022776108, 'fraction_numerical': 0.0020054433462254693, 'mean_word_length': 1.813053988718775, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1042312', 'n_tokens_mistral': 2228, 'n_tokens_neox': 2209, 'n_words': 452}