function nae_get_dataset (){ $args = array( 'posts_per_page' => -1, 'post_type' => 'post', 'post_status' => 'publish' ); $post_array = get_posts( $args ); $nodes = []; $edges = []; foreach ($post_array as $post) { $category = get_the_category($post->ID); $root_category_ID = array_pop(get_ancestors( $category[0]->cat_ID, 'category' )); $root_category = get_category($root_category_ID); $nodes[] = [ 'id' => $post->ID, 'label' => $post->post_name, 'group' => $root_category->slug, 'title' => ''.$post->post_title.'' ]; $html = do_shortcode($post->post_content); $dom = new DOMDocument; @$dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8')); $xpath = new DOMXPath($dom); $query = "//a[ @href != '' and not(starts-with(@href, '#')) and normalize-space() != '' ]"; foreach ($xpath->query($query) as $node) { $href = $xpath->evaluate('string(@href)', $node); $linked_post_id = url_to_postid($href); if ($linked_post_id != 0) { $edges[] = [ 'from' => $post->ID, 'to' => $linked_post_id ]; } } } return "[".json_encode($nodes).",".json_encode($edges)."]"; } function nae_echo_article_map(){ $dataset = nae_get_dataset (); $body = <<
EOD; return $body; } add_shortcode( 'show_article_map', 'nae_echo_article_map' );