Skip to content

Commit

Permalink
Issue #18 - implement navigation menu overrides to replace hardcoded …
Browse files Browse the repository at this point in the history
…links prefixed https://s.b.wp55/thisis
  • Loading branch information
bobbingwide committed May 4, 2021
1 parent fe35de7 commit 22dcef3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
4 changes: 2 additions & 2 deletions includes/block-overrides.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
require_once __DIR__ . '/template-part.php';

//require_once __DIR__ . '/navigation.php';
//require_once __DIR__ . '/navigation-link.php';
require_once __DIR__ . '/navigation-link.php';
//require_once __DIR__ . '/post-hierarchical-terms.php';
//require_once __DIR__ . '/block.php';
//require_once __DIR__ . '/tag-cloud.php';
Expand All @@ -47,7 +47,7 @@ function thisis_register_block_type_args( $args ) {
$args = thisis_maybe_override_block( $args,'core/template-part', 'render_block_core_template_part' );

//$args = thisis_maybe_override_block( $args,'core/navigation', 'render_block_core_navigation' );
//$args = thisis_maybe_override_block( $args,'core/navigation-link', 'render_block_core_navigation_link' );
$args = thisis_maybe_override_block( $args,'core/navigation-link', 'render_block_core_navigation_link' );
//$args = thisis_maybe_override_block( $args,'core/post-hierarchical-terms', 'render_block_core_post_hierarchical_terms' );
//$args = thisis_maybe_override_block( $args,'core/block', 'render_block_core_block' );
//$args = thisis_maybe_override_block( $args,'core/tag-cloud', 'render_block_core_tag_cloud' );
Expand Down
60 changes: 60 additions & 0 deletions includes/navigation-link.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/**
* Overrides core/navigation-link
*
* @param $attributes
* @param $content
* @param $block
* @return string
*/
function thisis_render_block_core_navigation_link($attributes, $content, $block)
{
$attributes = thisis_fiddle_nav_atts($attributes);
$html = gutenberg_render_block_core_navigation_link($attributes, $content, $block);
return $html;
}

/**
* Fiddles navigation-link attributes to get the required CSS classes.
*
* This is much simpler than the logic in `_wp_menu_item_classes_by_context( $menu_items )`.
*
* @TODO But, we also need to set the other classes such as current-menu-ancestor
* which probably means we have to do this in core/navigation rather than core/navigation-link
*
* And we should cater for the 404 page being displayed when the menu item didn't exist.
* @TODO Test what happens when the menu item becomes invalid.
*
* @param $attributes
* @return mixed
*/
function thisis_fiddle_nav_atts($attributes)
{
if (isset($attributes['url'])) {
$attributes['url'] = str_replace('https://s.b/wp55/thisis', site_url(), $attributes['url']);
$request_uri = $_SERVER["REQUEST_URI"];
$request_uri_path = parse_url($request_uri, PHP_URL_PATH);
$url_path = parse_url($attributes['url'], PHP_URL_PATH);
$site_url = trailingslashit(parse_url(site_url(), PHP_URL_PATH));

// We need to avoid the home page: home_url() or site_url()
if ($url_path === $site_url) {
if ($url_path === $request_uri_path) {
$attributes['className'] = ['current-menu-item'];
} else {
// don't match this $url path with all the other paths

}
} elseif (0 === strpos($request_uri, $url_path)) {

// @TODO check that the attributes URL is
//if ( parse_url( $request_uri, PHP_URL_PATH ) === parse_url( $attributes['url'], PHP_URL_PATH ) ) {
$attributes['className'] = ['current-menu-item'];
}
}
// Pragmatic workaround to problems when the id's not what Gutenberg thinks it is.
// Note: ID is no longer an attribute. So this is for backward compatibility.
unset($attributes['id']);
return $attributes;
}

0 comments on commit 22dcef3

Please sign in to comment.