| 1 |
<?php |
| 2 |
|
| 3 |
$twig = new Twig_Environment(new Twig_Loader_Filesystem([ |
| 4 |
dirname(dirname(__FILE__)) . '/views', |
| 5 |
dirname(dirname(__FILE__)) . '/public', |
| 6 |
]), ['autoescape' => false, 'debug' => false]); |
| 7 |
|
| 8 |
//$twig->addExtension(new Twig_Extension_Debug()); |
| 9 |
if(!is_admin()): |
| 10 |
|
| 11 |
$twig->addFilter(new Twig_SimpleFilter('shortcode', function($string) { |
| 12 |
return do_shortcode($string); |
| 13 |
})); |
| 14 |
|
| 15 |
$twig->addFilter(new Twig_SimpleFilter('translate', function($string, $key) { |
| 16 |
$translated = apply_filters('wpml_translate_single_string', $string, 'Responsive Menu', $key); |
| 17 |
$translated = function_exists('pll__') ? pll__($translated) : $translated; |
| 18 |
return $translated; |
| 19 |
})); |
| 20 |
|
| 21 |
$twig->addFunction(new Twig_SimpleFunction('build_menu', function($env, $options) { |
| 22 |
|
| 23 |
$translator = $env->getFilter('translate')->getCallable(); |
| 24 |
$menu = $translator($options['menu_to_use'], 'menu_to_use'); |
| 25 |
$walker = $options['custom_walker'] ? new $options['custom_walker']($options) : new ResponsiveMenu\Walkers\Walker($options); |
| 26 |
|
| 27 |
return wp_nav_menu( |
| 28 |
[ |
| 29 |
'container' => '', |
| 30 |
'menu_id' => 'responsive-menu', |
| 31 |
'menu_class' => null, |
| 32 |
'menu' => $menu && !$options['theme_location_menu'] ? $menu : null, |
| 33 |
'depth' => $options['menu_depth'] ? $options['menu_depth'] : 0, |
| 34 |
'theme_location' => $options['theme_location_menu'] ? $options['theme_location_menu'] : null, |
| 35 |
'walker' => $walker, |
| 36 |
'echo' => false |
| 37 |
] |
| 38 |
); |
| 39 |
|
| 40 |
}, ['needs_environment' => true])); |
| 41 |
|
| 42 |
$twig->addGlobal('search_url', function_exists('icl_get_home_url') ? icl_get_home_url() : get_home_url()); |
| 43 |
|
| 44 |
else: |
| 45 |
|
| 46 |
$twig->addFunction(new Twig_SimpleFunction('csrf', function() { |
| 47 |
return wp_nonce_field('update', 'responsive-menu-nonce', true, false); |
| 48 |
})); |
| 49 |
|
| 50 |
$twig->addFunction(new Twig_SimpleFunction('current_page', function() { |
| 51 |
return get_option('responsive_menu_current_page', 'initial-setup'); |
| 52 |
})); |
| 53 |
|
| 54 |
$twig->addFunction(new Twig_SimpleFunction('header_bar_items', function($items) { |
| 55 |
if(isset($items['button'])) |
| 56 |
unset($items['button']); |
| 57 |
return $items; |
| 58 |
})); |
| 59 |
|
| 60 |
$twig->addFunction(new Twig_SimpleFunction('menu_items', function($options) { |
| 61 |
|
| 62 |
if($options['theme_location_menu']) |
| 63 |
$menu = get_term(get_nav_menu_locations()[$options['theme_location_menu']], 'nav_menu')->name; |
| 64 |
elseif($options['menu_to_use']) |
| 65 |
$menu = $options['menu_to_use']; |
| 66 |
else |
| 67 |
$menu = get_terms('nav_menu')[0]->slug; |
| 68 |
|
| 69 |
return wp_get_nav_menu_items($menu); |
| 70 |
})); |
| 71 |
|
| 72 |
$twig->addFunction(new Twig_SimpleFunction('font_icons', function($array) { |
| 73 |
$new_array = []; |
| 74 |
for($i=0; $i < count($array['id']); $i++): |
| 75 |
$new_array[$i] = [ |
| 76 |
'id' => $array['id'][$i], |
| 77 |
'icon' => $array['icon'][$i], |
| 78 |
'type' => $array['type'][$i] |
| 79 |
]; |
| 80 |
endfor; |
| 81 |
return $new_array; |
| 82 |
})); |
| 83 |
|
| 84 |
$twig->addGlobal('admin_url', get_admin_url()); |
| 85 |
$twig->addGlobal('shortcode', '[responsive_menu]'); |
| 86 |
|
| 87 |
$twig->addFunction(new Twig_SimpleFunction('hide_pro_options', function() { |
| 88 |
return get_option('hide_pro_options', 'no'); |
| 89 |
})); |
| 90 |
|
| 91 |
endif; |
| 92 |
|
| 93 |
$twig->addFilter(new Twig_SimpleFilter('json_decode', function($string) { |
| 94 |
return json_decode($string, true); |
| 95 |
})); |
| 96 |
|
| 97 |
return $twig; |