Pressbooks\Book::getBookStructure PHP Метод

getBookStructure() статический публичный Метод

Returns an array representing the entire structure of a book, in correct order, with a minimum amount of fields. Data is raw and must be post-processed.
См. также: bottom of this file for more info
static public getBookStructure ( string $id = '' ) : array
$id string
Результат array
    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;
    }

Usage Example

Пример #1
0
/**
 * Displays a Book widget
 */
function display_book_widget()
{
    $book_structure = \PressBooks\Book::getBookStructure();
    // front-matter
    echo "<ul><li><h4>" . __('Front Matter', 'pressbooks') . "</h4></li><ul>";
    foreach ($book_structure['front-matter'] as $fm) {
        $title = !empty($fm['post_title']) ? $fm['post_title'] : '&hellip;';
        echo "<li class='front-matter'><a href='post.php?post=" . $fm['ID'] . "&action=edit'>" . $title . "</a></li>\n";
    }
    echo "</ul>";
    // parts
    foreach ($book_structure['part'] as $part) {
        $title = !empty($part['post_title']) ? $part['post_title'] : '&hellip;';
        echo "<ul><li><h4><a href='post.php?post=" . $part['ID'] . "&action=edit'>" . $title . "</a></h4></li><ul>\n";
        // chapters
        foreach ($part['chapters'] as $chapter) {
            $title = !empty($chapter['post_title']) ? $chapter['post_title'] : '&hellip;';
            echo "<li class='chapter'><a href='post.php?post=" . $chapter['ID'] . "&action=edit'>" . $title . "</a></li>\n";
        }
        echo "</ul>\n";
    }
    // back-matter
    echo "<li><h4>" . __('Back Matter', 'pressbooks') . "</h4></li><ul>";
    foreach ($book_structure['back-matter'] as $bm) {
        $title = !empty($bm['post_title']) ? $bm['post_title'] : '&hellip;';
        echo "<li class='back-matter'><a href='post.php?post=" . $bm['ID'] . "&action=edit'>" . $title . "</a></li>\n";
    }
    echo "</ul>";
    // add, organize
    echo "</ul>\n";
    echo '<div class="part-buttons"><a href="post-new.php?post_type=chapter">' . __('Add', 'pressbooks') . '</a> | <a class="remove" href="admin.php?page=pressbooks">' . __('Organize', 'pressbooks') . '</a></div>';
}
All Usage Examples Of Pressbooks\Book::getBookStructure