| 1 |
<?php |
| 2 |
/** |
| 3 |
* Resets menu locations to those from Settings → Headless → Menu Locations. |
| 4 |
* |
| 5 |
* @package FaustWP |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPE\FaustWP\Menus; |
| 9 |
|
| 10 |
use function WPE\FaustWP\Settings\faustwp_get_setting; |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
add_action( 'after_setup_theme', __NAMESPACE__ . '\\remove_menu_locations', 100 ); |
| 17 |
/** |
| 18 |
* Callback for WordPress 'after_setup_theme' action. |
| 19 |
* |
| 20 |
* Unregisters menu locations such as those provided by the active PHP theme. |
| 21 |
*/ |
| 22 |
function remove_menu_locations() { |
| 23 |
$menus = array_keys( get_registered_nav_menus() ); |
| 24 |
|
| 25 |
array_walk( $menus, 'unregister_nav_menu' ); |
| 26 |
} |
| 27 |
|
| 28 |
add_action( 'after_setup_theme', __NAMESPACE__ . '\\register_menu_locations', 101 ); |
| 29 |
/** |
| 30 |
* Callback for WordPress 'after_setup_theme' action. |
| 31 |
* |
| 32 |
* Registers menus specified at Settings → Headless → Menu Locations. |
| 33 |
*/ |
| 34 |
function register_menu_locations() { |
| 35 |
$location_setting = faustwp_get_setting( 'menu_locations', 'Primary, Footer' ); |
| 36 |
$locations = explode( ',', $location_setting ); |
| 37 |
$menus = array(); |
| 38 |
|
| 39 |
foreach ( $locations as $location ) { |
| 40 |
$location = trim( $location ); |
| 41 |
$key = sanitize_title_with_dashes( $location ); |
| 42 |
|
| 43 |
if ( $key ) { |
| 44 |
$menus[ $key ] = $location; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
if ( $menus ) { |
| 49 |
register_nav_menus( $menus ); |
| 50 |
} |
| 51 |
} |
| 52 |
|