Skip to content

Commit

Permalink
クラシックナビも同一クラス化
Browse files Browse the repository at this point in the history
  • Loading branch information
kurudrive committed Jan 8, 2025
1 parent 59d2bfc commit 1be14ea
Showing 1 changed file with 52 additions and 125 deletions.
177 changes: 52 additions & 125 deletions inc/nav-menu-class-custom.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<?php
/**
* VkNavMenuClassCustom
*
* @package vektor-inc/vk-all-in-one-expansion-unit
*/

if ( ! class_exists( 'VkNavMenuClassCustom' ) ) {

class VkNavMenuClassCustom {
Expand All @@ -18,9 +24,30 @@ public static function init() {
* @return array $classes : メニューのクラス配列
*/
public static function add_current_class_to_classic_navi( $classes, $item ) {
// 付与するカレントクラス名
$add_current_class_name = 'current-menu-ancestor';

if ( self::is_active_menu_item( $item->url ) ) {
$classes[] = 'current-menu-ancestor';
$classes[] = $add_current_class_name;
} else {
// 投稿のトップのメニューアイテムは、カスタム投稿タイプのページを表示していても
// 勝手に current クラスが付与されるので、
// アクティブでないと判断された項目に current クラスがあった場合は削除する必要がある

// 現在配列に入っているclassをループ
for ( $i = 1; $i <= count( $classes ); $i++ ) {
if ( isset( $classes[ $i ] ) ) {
// currentがあった場合
if ( $classes[ $i ] == $add_current_class_name || $classes[ $i ] == 'current_page_parent' ) {
// そのクラスをキーごと削除
unset( $classes[ $i ] );
}
}
}
}

// キーのフリなおし
$classes = array_values( $classes );
return $classes;
}

Expand Down Expand Up @@ -112,20 +139,32 @@ public static function get_post_type_from_url( $url ) {
}

/**
* カレントメニューアイテムかどうかを判定する
* Get displaying page post type slug
*
* @param array $item_src : メニューアイテムの属性
* @return bool : カレントメニューアイテムかどうか
* @return string : post type slug
*/
public static function is_active_menu_item( $item_src ) {
$return = false;
$displaying_page_post_type_info = array();
public static function get_displaying_page_post_type_slug() {
// 今表示しているページが属する投稿タイプを取得
if ( function_exists( 'vk_get_post_type' ) ) {
$displaying_page_post_type_info = vk_get_post_type();
$displaying_page_post_type_slug = $displaying_page_post_type_info['slug'];
} else {
$displaying_page_post_type_info['slug'] = get_post_type();
$displaying_page_post_type_slug = get_post_type();
}
return $displaying_page_post_type_slug;
}

/**
* カレントメニューアイテムかどうかを判定する
*
* @param array $item_src : メニューアイテムの属性
* @return bool : カレントメニューアイテムかどうか
*/
public static function is_active_menu_item( $item_src ) {
$return = false;

// 今表示しているページが属する投稿タイプを取得
$displaying_page_post_type_slug = self::get_displaying_page_post_type_slug();

/*
投稿アーカイブの指定された固定ページメニューアイテムの処理
Expand All @@ -135,16 +174,17 @@ public static function is_active_menu_item( $item_src ) {

// 投稿トップのメニューアイテム
if ( $post_top_url === $item_src ) {
if ( $displaying_page_post_type_info['slug'] === 'post' ) {
if ( $displaying_page_post_type_slug === 'post' ) {
// 今表示しているページの投稿タイプが post の場合
$return = true;
}
}

$menu_url_post_type = self::get_post_type_from_url( $item_src );
// 今表示しているページの投稿タイプとメニューに記入されているURLの投稿タイプが同じ場合
if ( isset( $menu_url_post_type ) && isset( $displaying_page_post_type_info['slug'] ) ) {
if ( $displaying_page_post_type_info['slug'] === $menu_url_post_type ) {

if ( ! empty( $menu_url_post_type ) && ! empty( $displaying_page_post_type_slug ) ) {
// 今表示しているページの投稿タイプとメニューに記入されているURLの投稿タイプが同じ場合
if ( $displaying_page_post_type_slug === $menu_url_post_type ) {
$return = true;
}
}
Expand All @@ -156,116 +196,3 @@ public static function is_active_menu_item( $item_src ) {
}

VkNavMenuClassCustom::init();

add_filter( 'nav_menu_css_class', 'veu_add_current_class_to_classic_nav', 10, 2 );
function veu_add_current_class_to_classic_nav( $classes, $item ) {

// 今表示しているページが属する投稿タイプを取得
if ( function_exists( 'vk_get_post_type' ) ) {
$post_type_info = vk_get_post_type();
} else {
$post_type_info['slug'] = get_post_type();
}

// 付与するカレントクラス名
$add_current_class_name = 'current-menu-ancestor';

/*
投稿アーカイブの指定された固定ページメニューアイテムの処理
/*-------------------------------------------*/
$post_top_id = get_option( 'page_for_posts' );
$post_top_url = get_the_permalink( $post_top_id );

if ( $post_top_url === $item->url ) {
if ( $post_type_info['slug'] === 'post' ) {
// 今表示しているページの投稿タイプが post の場合
// currentクラスを付与
$classes[] = $add_current_class_name;
} else {
// 今表示しているページの投稿タイプが post 以外の場合

// 現在配列に入っているclassをループ
for ( $i = 1; $i <= count( $classes ); $i++ ) {
if ( isset( $classes[ $i ] ) ) {
// currentがあった場合
if ( $classes[ $i ] == $add_current_class_name || $classes[ $i ] == 'current_page_parent' ) {
// そのクラスをキーごと削除
unset( $classes[ $i ] );
}
}
}

// キーのフリなおし
$classes = array_values( $classes );
}
}

/*
カスタムメニューに設定されたURLの投稿タイプ名を取得する
/*-------------------------------------------*/

// メニューがカスタムリンクでリンク先がカスタム投稿タイプのアーカイブの時

if ( $item->type == 'custom' || $item->type == 'post_type_archive' ) {

// リライトルールを取得
$rewrite_rules = get_option( 'rewrite_rules' );

if ( ! $rewrite_rules || ! is_array( $rewrite_rules ) ) {

// リライトルールを無指定で使っている場合

$pattern = '/.*post_type=(.*)/';
$subject = $item->url;
preg_match( $pattern, $subject, $matches );

// メニューの投稿タイプが取得できたら
if ( isset( $matches[1] ) ) {
$menu_url_post_type = $matches[1];
} else {
$menu_url_post_type = '';
}// if ( isset( $matches[1] ) ) {
} else {

// リライトルールが普通に保存されている場合

// リライトルールをループ
foreach ( $rewrite_rules as $key => $value ) {

// メニューに記載されているURLから投稿タイプ名を判別する

// ループ中のりライトルールがメニューのURLと合致するか正規表現で検出
$pattern = '{' . $key . '}';
$subject = $item->url;
preg_match( $pattern, $subject, $matches );

// マッチした場合
if ( $matches ) {

// マッチした $value の URL ( index.php?post_type=custom など ) から投稿タイプが判別できる
// 正規表現で post_type= の値を抽出する

$pattern = '/index.php\?post_type=(.*)/';
$subject = $value;
preg_match( $pattern, $subject, $matches );

// メニューの投稿タイプが取得できたら
if ( isset( $matches[1] ) ) {
$menu_url_post_type = $matches[1];
// 最初にマッチしてクラスを付与したら抜ける
break;
} // if ( isset( $matches[1] ) ) {
} // if ( $matches ) {
} // foreach ( $rewrite_rules as $key => $value ) {
}

// 今表示しているページの投稿タイプとメニューに記入されているURLの投稿タイプが同じ場合
if ( isset( $menu_url_post_type ) && isset( $post_type_info['slug'] ) ) {
if ( $post_type_info['slug'] === $menu_url_post_type ) {
$classes[] = $add_current_class_name;
}
}
} // if ( $item->object == 'custom' && $item->type == 'post_type_archive' ) {

return $classes;
}

0 comments on commit 1be14ea

Please sign in to comment.