Written by Anonymous
if ( !function_exists( 'get_toc_tag' ) ):
function get_toc_tag($expanded_content, &$harray, $is_widget = false, $depth_option = 0){
global $post;
$mode = true;
//フォーラムページだと表示しない
if (is_plugin_fourm_page()) {
return;
}
$content = $expanded_content;
if ($mode) {
$content = $post->post_content;
$pages = explode('<!--nextpage-->', $content);
}
$headers = array();
$html = '';
$toc_list = '';
$id = '';
$toggle = '';
$counter = 0;
$counters = array(0,0,0,0,0,0);
$harray = array();
$class = 'toc';
$title = get_toc_title(); //目次タイトル
$showcount = 0;
$depth = intval(get_toc_depth()); //2-6 0で全て
$top_level = 2; //h2がトップレベル
$targetclass = 'entry-content'; //目次対象となるHTML要素
if ($title === '') {
$title = __('目次', THEME_NAME);
}
$set_depth = intval(get_toc_depth()); //2-6 0で全て
if (intval($set_depth) == 0) {
$set_depth = 6;
}
$number_visible = is_toc_number_visible(); //見出しの数字を表示するか
if ($number_visible) {
$list_tag = 'ol';
} else {
$list_tag = 'ul';
}
if($targetclass===''){$targetclass = get_post_type();}
for($h = $top_level; $h <= 6; $h++){$harray[] = 'h' . $h . '';}
if ($mode) {
// 分割ページごとに見出しを取得
$page_num = 1;
foreach ($pages as $page_content) {
if (preg_match_all('/<([hH][2-6]).*?>(.*?)<\/[hH][2-6]>/is', $page_content, $matches, PREG_SET_ORDER)) {
foreach ($matches as $m) {
$counter++;
$headers[] = array(
'tag' => strtolower($m[1]),
'text' => trim(strip_tags($m[2])),
'raw' => $m[2],
'id' => 'toc'.$counter,
'page' => $page_num,
);
}
}
$page_num++;
}
$header_count = count($headers);
}else {
preg_match_all('/<([hH][1-6]).*?>(.*?)<\/[hH][1-6].*?>/us', $content, $headers);
$header_count = count($headers[0]);
}
if($top_level < 1){$top_level = 1;}
if($top_level > 6){$top_level = 6;}
$current_depth = $top_level - 1;
$prev_depth = $top_level - 1;
$max_depth = (($depth == 0) ? 6 : intval($depth)) - $top_level + 1;
if($header_count > 0){
$toc_list .= '<' . $list_tag . (($current_depth == $top_level - 1) ? ' class="toc-list open"' : '') . '>';
}
for($i=0;$i < $header_count;$i++){
$depth = 0;
$h_actual = $mode ? $headers[$i]['tag'] : strtolower($headers[1][$i]);
switch($h_actual) {
case 'h1': $depth = 1 - $top_level + 1; break;
case 'h2': $depth = 2 - $top_level + 1; break;
case 'h3': $depth = 3 - $top_level + 1; break;
case 'h4': $depth = 4 - $top_level + 1; break;
case 'h5': $depth = 5 - $top_level + 1; break;
case 'h6': $depth = 6 - $top_level + 1; break;
}
if($depth >= 1 && $depth <= $max_depth){
if($current_depth == $depth && $i != 0){
$toc_list .= '</li>';
$counters[$current_depth - 1] ++;
}
while($current_depth > $depth){
$toc_list .= '</li></'.$list_tag.'>';
$current_depth--;
$counters[$current_depth] = 0;
$counters[$current_depth - 1] ++;
}
if($current_depth != $prev_depth){
$toc_list .= '</li>';
$counters[$current_depth - 1] ++;
}
while($current_depth < $depth){
$toc_list .= '<'.$list_tag.'>';
$current_depth++;
$counters[$current_depth - 1] ++;
}
$hide_class = null;
if ( $depth_option != 0 && $depth >= $depth_option ) {
$hide_class = ' class="display-none"';
}
$counter++;
$text = $mode ? strip_tags($headers[$i]['text']) : strip_tags($headers[2][$i]);
if (is_toc_heading_inner_html_tag_enable()) {
$text = $mode ? $headers[$i]['raw'] : $headers[2][$i];
$text = preg_replace('{<a.*?>(.*?)</a>}', "$1", $text);
}
if ($mode) {
$link = get_permalink($post);
if ($headers[$i]['page'] > 1) $link = trailingslashit($link).$headers[$i]['page'].'/';
$toc_list .= '<li'.$hide_class.'><a href="'.$link.'#'.$headers[$i]['id'].'" tabindex="0">' . $text . '</a>';
}else {
$toc_list .= '<li'.$hide_class.'><a href="#toc' . $counter . '" tabindex="0">' . $text . '</a>';
}
$prev_depth = $depth;
}
}
while($current_depth >= 1 ){
$toc_list .= '</li></'.$list_tag.'>';
$current_depth--;
}
///////////////////////////////////////////
// 目次タグの生成
///////////////////////////////////////////
if($id!==''){$id = ' id="' . $id . '"';}else{$id = '';}
if (is_toc_toggle_switch_enable()) {
$checked = null;
$is_visible = apply_filters('is_toc_content_visible', is_toc_content_visible());
if ($is_visible) {
$checked = ' checked';
}
$title_elm = 'label';
// if ($is_widget) {
// $toc_check = null;
// $label_for = null;
// } else {
// global $_TOC_INDEX;
// $toc_id = 'toc-checkbox-'.$_TOC_INDEX;
// $toc_check = '<input type="checkbox" class="toc-checkbox" id="'.$toc_id.'"'.$checked.'>';
// $label_for = ' for="'.$toc_id.'"';
// $_TOC_INDEX++;
// }
global $_TOC_INDEX;
$toc_id = 'toc-checkbox-'.$_TOC_INDEX;
$toc_check = '<input type="checkbox" class="toc-checkbox" id="'.$toc_id.'"'.$checked.'>';
$label_for = ' for="'.$toc_id.'"';
$_TOC_INDEX++;
} else {
$title_elm = 'div';
$toc_check = null;
$label_for = null;
}
$html .= '
<div' . $id . ' class="' . $class . get_additional_toc_classes() . ' border-element">'.$toc_check.
'<'.$title_elm.' class="toc-title"'.$label_for.'>' . $title . '</'.$title_elm.'>
<div class="toc-content">
' . $toc_list .'
</div>
</div>';
global $_TOC_AVAILABLE_H_COUNT;
$_TOC_AVAILABLE_H_COUNT = $counter;
if (!is_toc_display_count_available($counter)){
return ;
}
return apply_filters('get_toc_tag',$html, $harray, $is_widget );
}
endif;