diff --git a/inc/admin/namespace.php b/inc/admin/namespace.php index 84f1822..bdacbb8 100644 --- a/inc/admin/namespace.php +++ b/inc/admin/namespace.php @@ -6,6 +6,7 @@ namespace Aldine\Admin; use PressbooksMix\Assets; +use Pressbooks\DataCollector\Book as BookDataCollector; /** * Uses old option to provide a simpler upgrade path from pressbooks-publisher theme @@ -47,8 +48,10 @@ function update_catalog() { if ( $in_catalog === 'true' ) { update_blog_option( $blog_id, \Aldine\Admin\BLOG_OPTION, 1 ); + update_site_meta( $blog_id, BookDataCollector::IN_CATALOG, 1 ); } else { delete_blog_option( $blog_id, \Aldine\Admin\BLOG_OPTION ); + update_site_meta( $blog_id, BookDataCollector::IN_CATALOG, 0 ); } } diff --git a/inc/helpers/namespace.php b/inc/helpers/namespace.php index cdb448e..122229b 100644 --- a/inc/helpers/namespace.php +++ b/inc/helpers/namespace.php @@ -10,7 +10,7 @@ namespace Aldine\Helpers; use function \Pressbooks\Metadata\book_information_to_schema; use function \Pressbooks\Metadata\is_bisac; use function \Pressbooks\Utility\str_starts_with; -use Pressbooks\Book; +use Pressbooks\DataCollector\Book as BookDataCollector; /** * @param int $page @@ -56,16 +56,14 @@ function get_catalog_data( $page = 1, $per_page = 10, $orderby = 'title', $licen /** @var \WP_Site $site */ + $dc = BookDataCollector::init(); $sites_in_catalog = []; - $sites = new \WP_Site_Query( $args ); - foreach ( $sites->sites as $site ) { - // TODO: Using switch_to_blog() is a performance problem. Use [ https://core.trac.wordpress.org/ticket/37923 ] when available. - switch_to_blog( $site->blog_id ); - if ( get_option( \Aldine\Admin\BLOG_OPTION ) ) { - $site->pb_title = get_bloginfo( 'name' ); // Cool hack! :face_with_rolling_eyes: + $sites = get_sites( $args ); + foreach ( $sites as $site ) { + if ( get_site_meta( $site->blog_id, $dc::IN_CATALOG, true ) ) { + $site->pb_title = $dc->get( $site->blog_id, $dc::TITLE, ); // Cool hack! :face_with_rolling_eyes: $sites_in_catalog[] = $site; } - restore_current_blog(); } if ( $orderby === 'latest' ) { $sites_in_catalog = wp_list_sort( $sites_in_catalog, 'last_updated', 'DESC' ); @@ -81,15 +79,16 @@ function get_catalog_data( $page = 1, $per_page = 10, $orderby = 'title', $licen continue; } - switch_to_blog( $site->blog_id ); - $schema = book_information_to_schema( Book::getBookInformation() ); - $book['title'] = $schema['name']; - $book['date-published'] = $schema['datePublished'] ?? ''; - $book['subject'] = $schema['about'][0]['identifier'] ?? ''; - $book['link'] = get_blogaddress_by_id( $site->blog_id ); - $book['metadata'] = $schema; - $books[] = $book; - restore_current_blog(); + $book_information = $dc->get( $site->blog_id, $dc::BOOK_INFORMATION_ARRAY ); + if ( is_array( $book_information ) && ! empty( $book_information ) ) { + $schema = book_information_to_schema( $book_information ); + $book['title'] = $schema['name']; + $book['date-published'] = $schema['datePublished'] ?? ''; + $book['subject'] = $schema['about'][0]['identifier'] ?? ''; + $book['link'] = get_blogaddress_by_id( $site->blog_id ); + $book['metadata'] = $schema; + $books[] = $book; + } if ( count( $books ) >= $per_page ) { break;