<commit_before><?php
/**
 * Register menu
 *
 * @see https://developer.wordpress.org/rest-api/extending-the-rest-api/routes-and-endpoints/
 */

use DcoupledSupport\UrlUtils;

/**
 * Class RestMenus
 */
class RestMenus
{
    const API_NAMESPACE = 'wp/v2';


    /**
     * Register menus route.
     * @return void
     */
    public function registerRoutes()
    {

        register_rest_route(static::API_NAMESPACE, '/menus', [
            [
                'methods' => WP_REST_Server::READABLE,
                'callback' => [$this, 'getMenus'],
            ]
        ]);

        add_filter('rest_menus_get_active_languages', [$this, 'getDefaultLanguageFilter'], 1);

    }

    public function getDefaultLanguageFilter($languages) {

        $locale = get_locale();

        if (strpos($locale, '_') === false) {
            return $locale;
        }

        return [explode('_', $locale)[0]];

    }


    /**
     * Get menus.
     *
     * @return array All registered menus
     */
    public function getMenus($request)
    {

        $lang            = $request->get_param('lang');
        $locations       = get_nav_menu_locations();
        $registeredMenus = get_registered_nav_menus();

        if (isset($lang)) {
            do_action('wpml_switch_language', $lang);
        }

        if (empty($lang)) {
            $languages = $this->getDefaultLanguageFilter('');
        } else if ($lang == 'all') {
            $languages = apply_filters('rest_menus_get_active_languages', '');
        } else {
            $languages = explode(',', $lang);
        }

        $menus = [];

        foreach ($registeredMenus as $locationName => $label) {
            if (!isset($locations[$locationName])) {
                continue;
            }

            foreach ($languages as $language) {
                $menu = $this->getMenu($locations[$locationName], $language);

                if (empty($menu['count'])) {
                    continue;
                }

                $menus[$locationName][$language] = $menu;
            }

        }

        $rest_menus = [
            'result' => [
                'menus' => $menus,
            ],
        ];

        return rest_ensure_response($rest_menus);
    }

    function getMenu($locationId, $language) {

        if (!empty($language)) {
            do_action('wpml_switch_language', $language);
        }

        $menu = wp_get_nav_menu_object($locationId);<commit_msg>Fixes PHP notices for empty menus<commit_after><?php
/**
 * Register menu
 *
 * @see https://developer.wordpress.org/rest-api/extending-the-rest-api/routes-and-endpoints/
 */

use DcoupledSupport\UrlUtils;

/**
 * Class RestMenus
 */
class RestMenus
{
    const API_NAMESPACE = 'wp/v2';


    /**
     * Register menus route.
     * @return void
     */
    public function registerRoutes()
    {

        register_rest_route(static::API_NAMESPACE, '/menus', [
            [
                'methods' => WP_REST_Server::READABLE,
                'callback' => [$this, 'getMenus'],
            ]
        ]);

        add_filter('rest_menus_get_active_languages', [$this, 'getDefaultLanguageFilter'], 1);

    }

    public function getDefaultLanguageFilter($languages) {

        $locale = get_locale();

        if (strpos($locale, '_') === false) {
            return $locale;
        }

        return [explode('_', $locale)[0]];

    }


    /**
     * Get menus.
     *
     * @return array All registered menus
     */
    public function getMenus($request)
    {

        $lang            = $request->get_param('lang');
        $locations       = get_nav_menu_locations();
        $registeredMenus = get_registered_nav_menus();

        if (isset($lang)) {
            do_action('wpml_switch_language', $lang);
        }

        if (empty($lang)) {
            $languages = $this->getDefaultLanguageFilter('');
        } else if ($lang == 'all') {
            $languages = apply_filters('rest_menus_get_active_languages', '');
        } else {
            $languages = explode(',', $lang);
        }

        $menus = [];

        foreach ($registeredMenus as $locationName => $label) {
            if (!isset($locations[$locationName])) {
                continue;
            }

            foreach ($languages as $language) {
                $menu = $this->getMenu($locations[$locationName], $language);

                if (empty($menu['count'])) {
                    continue;
                }

                $menus[$locationName][$language] = $menu;
            }

        }

        $rest_menus = [
            'result' => [
                'menus' => $menus,
            ],
        ];

        return rest_ensure_response($rest_menus);
    }

    function getMenu($locationId, $language) {

        if (!empty($language)) {
            do_action('wpml_switch_language', $language);
        }

        $menu = wp_get_nav_menu_object($locationId);
        
        if ($menu == false) {
            return [];
        }
{'fraction_non_alphanumeric': 0.1429718875502008, 'fraction_numerical': 0.0012048192771084338, 'mean_word_length': 1.7780256553262688, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 2, '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': '4588499', 'n_tokens_mistral': 1505, 'n_tokens_neox': 1412, 'n_words': 240}