static function getBookStructure($id = '')
{
// -----------------------------------------------------------------------------
// Is cached?
// -----------------------------------------------------------------------------
if (!empty($id) && is_int($id)) {
// @codingStandardsIgnoreLine
$blog_id = $id;
switch_to_blog($id);
} else {
global $blog_id;
}
$cache_id = "book-str-{$blog_id}";
$book_structure = wp_cache_get($cache_id, 'pb');
if ($book_structure) {
return $book_structure;
}
// -----------------------------------------------------------------------------
// Query our custom post types, keep minimal data in $book_structure
// -----------------------------------------------------------------------------
$book_structure = array();
$custom_types = array('front-matter', 'part', 'chapter', 'back-matter');
$q = new \WP_Query();
foreach ($custom_types as $type) {
$book_structure[$type] = array();
$args = array('post_type' => $type, 'posts_per_page' => -1, 'post_status' => 'any', 'orderby' => 'menu_order', 'order' => 'ASC', 'no_found_rows' => true, 'cache_results' => true);
$results = $q->query($args);
foreach ($results as $post) {
$post_name = static::fixSlug($post->post_name);
$book_structure[$type][] = array('ID' => $post->ID, 'post_title' => $post->post_title, 'post_name' => $post_name, 'post_author' => $post->post_author, 'comment_count' => $post->comment_count, 'menu_order' => $post->menu_order, 'post_status' => $post->post_status, 'export' => get_post_meta($post->ID, 'pb_export', true) ? true : false, 'post_parent' => $post->post_parent);
}
}
// -----------------------------------------------------------------------------
// Add Chapters to Parts
// -----------------------------------------------------------------------------
foreach ($book_structure['part'] as $i => $part) {
$book_structure['part'][$i]['chapters'] = array();
}
foreach ($book_structure['chapter'] as $i => $chapter) {
foreach ($book_structure['part'] as $j => $part) {
if ($part['ID'] == $chapter['post_parent']) {
$book_structure['part'][$j]['chapters'][] = $chapter;
unset($book_structure['chapter'][$i]);
continue 2;
}
}
}
/* Track unexpected orphans, unset() chapter from $book_structure and $types */
if (count($book_structure['chapter'])) {
$book_structure['__orphans'] = $book_structure['chapter'];
}
unset($book_structure['chapter']);
$custom_types = array_diff($custom_types, array('chapter'));
// -----------------------------------------------------------------------------
// Create __order and __lookup arrays, remove post_parent
// -----------------------------------------------------------------------------
$book_structure['__order'] = array();
$book_structure['__export_lookup'] = array();
foreach ($custom_types as $type) {
foreach ($book_structure[$type] as $i => $struct) {
unset($book_structure[$type][$i]['post_parent']);
if ('part' != $type) {
$book_structure['__order'][$struct['ID']] = array('export' => $struct['export'], 'post_status' => $struct['post_status']);
if ($struct['export']) {
$book_structure['__export_lookup'][$struct['post_name']] = $type;
}
} else {
foreach ($struct['chapters'] as $j => $chapter) {
unset($book_structure[$type][$i]['chapters'][$j]['post_parent']);
if (get_post_meta($struct['ID'], 'pb_part_content', true) && get_post_meta($struct['ID'], 'pb_part_invisible', true) !== 'on') {
$book_structure['__order'][$struct['ID']] = array('export' => $struct['export'], 'post_status' => $struct['post_status']);
}
$book_structure['__order'][$chapter['ID']] = array('export' => $chapter['export'], 'post_status' => $chapter['post_status']);
if ($chapter['export']) {
$book_structure['__export_lookup'][$chapter['post_name']] = 'chapter';
}
}
}
}
}
// -----------------------------------------------------------------------------
// Cache & Return
// -----------------------------------------------------------------------------
wp_cache_set($cache_id, $book_structure, 'pb', 86400);
if (!empty($id) && is_int($id)) {
restore_current_blog();
}
return $book_structure;
}