Created
October 24, 2019 07:54
-
-
Save Liroo/972f5561eb75881f9706276c5a6c9fe4 to your computer and use it in GitHub Desktop.
FP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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