Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Liroo
Created October 24, 2019 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Liroo/972f5561eb75881f9706276c5a6c9fe4 to your computer and use it in GitHub Desktop.
Save Liroo/972f5561eb75881f9706276c5a6c9fe4 to your computer and use it in GitHub Desktop.
FP
<?php
defined('ABSPATH') || exit;
class _FP_Nav {
public static $nav = false;
public static $instance = null;
function __construct() {
self::$instance = $this;
$this->generate_nav_tree();
}
public function get_nav_tree() {
return self::$nav;
}
public function search_nav_tree($slug, $tree) {
if (!$tree) {
$tree = self::$nav;
} else if (isset($tree->child_items)) {
$tree = $tree->child_items;
}
foreach ($tree as $key => $node) {
$node_slug = $node->object == 'custom' ? $node->post_name : get_post_field('post_name', $node->object_id);
if ($node_slug == $slug) {
return $node;
}
}
return false;
}
public function generate_nav_tree() {
self::$nav = $this->nav_to_tree();
}
public function nav_to_tree() {
$nav_items = wp_get_nav_menu_items( 'main', array( 'update_post_term_cache' => false ) );
// wordpress does not group child nav items with parent nav items
$child_items = [];
// pull all child nav items into separate object
foreach ($nav_items as $key => $item) {
if ($item->menu_item_parent) {
array_push($child_items, $item);
unset($nav_items[$key]);
}
}
// push child items into their parent item in the original object
// May repeat the loop if a child is found before the parent
while (count($child_items)) {
foreach ($child_items as $key => $child_item) {
if ($this->nav_push_parent($nav_items, $child_item)) {
unset($child_items[$key]);
}
}
}
return array_values($nav_items);
}
public function nav_push_parent(&$parents, $child) {
foreach ($parents as $key => $item) {
if ($child->menu_item_parent == $item->ID) {
if (!$item->child_items) {
$item->child_items = [];
}
array_push($item->child_items, $child);
return true;
}
if ($item->child_items) {
if ($this->nav_push_parent($item->child_items, $child)) {
return true;
}
}
}
return false;
}
}
function FP_Nav() {
if (!(_FP_Nav::$instance)) {
return new _FP_Nav();
} else {
return _FP_Nav::$instance;
}
}
<?php
defined('ABSPATH') || exit;
class _FP_Router {
public static $instance = null;
public static $route = null;
function __construct() {
self::$instance = $this;
add_action('do_parse_request', array($this, 'do_parse_request'), 10, 2);
}
public function get_current_route() {
return self::$route;
}
public function do_parse_request($do, $wp) {
global $wp_query;
$path = trim(parse_url(esc_url_raw(add_query_arg([])), PHP_URL_PATH), '/');
$home_path = trim(parse_url(esc_url_raw(home_url()), PHP_URL_PATH), '/');
$home_path and $path = trim(substr($path, strlen($home_path)), '/');
$found = true;
$path = explode('/', $path);
if (!empty($path)) {
$prev_node = null;
$node = null;
foreach ($path as $path_item) {
$node = FP_Nav()->search_nav_tree($path_item, $node);
if ($node->object != 'custom') {
$node = $prev_node;
$found = true;
break;
}
if (!$node) {
$found = false;
break;
}
$prev_node = $node;
}
self::$route = $node;
}
if ($found) {
// If found, set the pagename to home for the correct behavior of wp
$wp->query_vars['pagename'] = 'home';
$do = false;
}
return $do;
}
}
function FP_Router() {
if (!(_FP_Router::$instance)) {
return new _FP_Router();
} else {
return _FP_Router::$instance;
}
}
<?php
/*
Require every files from ./inc/
*/
foreach (glob(__DIR__ . "/inc/*") as $filename) {
if (!is_dir($filename)) {
require_once($filename);
}
}
add_theme_support( 'post-thumbnails' );
FP_Nav();
FP_Router();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment