Browse Source

Use wp_blogmeta to speed up catalog (#192)

Use wp_blogmeta to speed up catalog
Fixes TODO in reference to:  https://core.trac.wordpress.org/ticket/37923
Depends on https://github.com/pressbooks/pressbooks/pull/1805
pull/193/head
Dac Chartrand 5 years ago committed by GitHub
parent
commit
139d01f558
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      inc/admin/namespace.php
  2. 33
      inc/helpers/namespace.php

3
inc/admin/namespace.php

@ -6,6 +6,7 @@
namespace Aldine\Admin; namespace Aldine\Admin;
use PressbooksMix\Assets; use PressbooksMix\Assets;
use Pressbooks\DataCollector\Book as BookDataCollector;
/** /**
* Uses old option to provide a simpler upgrade path from pressbooks-publisher theme * Uses old option to provide a simpler upgrade path from pressbooks-publisher theme
@ -47,8 +48,10 @@ function update_catalog() {
if ( $in_catalog === 'true' ) { if ( $in_catalog === 'true' ) {
update_blog_option( $blog_id, \Aldine\Admin\BLOG_OPTION, 1 ); update_blog_option( $blog_id, \Aldine\Admin\BLOG_OPTION, 1 );
update_site_meta( $blog_id, BookDataCollector::IN_CATALOG, 1 );
} else { } else {
delete_blog_option( $blog_id, \Aldine\Admin\BLOG_OPTION ); delete_blog_option( $blog_id, \Aldine\Admin\BLOG_OPTION );
update_site_meta( $blog_id, BookDataCollector::IN_CATALOG, 0 );
} }
} }

33
inc/helpers/namespace.php

@ -10,7 +10,7 @@ namespace Aldine\Helpers;
use function \Pressbooks\Metadata\book_information_to_schema; use function \Pressbooks\Metadata\book_information_to_schema;
use function \Pressbooks\Metadata\is_bisac; use function \Pressbooks\Metadata\is_bisac;
use function \Pressbooks\Utility\str_starts_with; use function \Pressbooks\Utility\str_starts_with;
use Pressbooks\Book; use Pressbooks\DataCollector\Book as BookDataCollector;
/** /**
* @param int $page * @param int $page
@ -56,16 +56,14 @@ function get_catalog_data( $page = 1, $per_page = 10, $orderby = 'title', $licen
/** @var \WP_Site $site */ /** @var \WP_Site $site */
$dc = BookDataCollector::init();
$sites_in_catalog = []; $sites_in_catalog = [];
$sites = new \WP_Site_Query( $args ); $sites = get_sites( $args );
foreach ( $sites->sites as $site ) { foreach ( $sites as $site ) {
// TODO: Using switch_to_blog() is a performance problem. Use [ https://core.trac.wordpress.org/ticket/37923 ] when available. if ( get_site_meta( $site->blog_id, $dc::IN_CATALOG, true ) ) {
switch_to_blog( $site->blog_id ); $site->pb_title = $dc->get( $site->blog_id, $dc::TITLE, ); // Cool hack! :face_with_rolling_eyes:
if ( get_option( \Aldine\Admin\BLOG_OPTION ) ) {
$site->pb_title = get_bloginfo( 'name' ); // Cool hack! :face_with_rolling_eyes:
$sites_in_catalog[] = $site; $sites_in_catalog[] = $site;
} }
restore_current_blog();
} }
if ( $orderby === 'latest' ) { if ( $orderby === 'latest' ) {
$sites_in_catalog = wp_list_sort( $sites_in_catalog, 'last_updated', 'DESC' ); $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; continue;
} }
switch_to_blog( $site->blog_id ); $book_information = $dc->get( $site->blog_id, $dc::BOOK_INFORMATION_ARRAY );
$schema = book_information_to_schema( Book::getBookInformation() ); if ( is_array( $book_information ) && ! empty( $book_information ) ) {
$book['title'] = $schema['name']; $schema = book_information_to_schema( $book_information );
$book['date-published'] = $schema['datePublished'] ?? ''; $book['title'] = $schema['name'];
$book['subject'] = $schema['about'][0]['identifier'] ?? ''; $book['date-published'] = $schema['datePublished'] ?? '';
$book['link'] = get_blogaddress_by_id( $site->blog_id ); $book['subject'] = $schema['about'][0]['identifier'] ?? '';
$book['metadata'] = $schema; $book['link'] = get_blogaddress_by_id( $site->blog_id );
$books[] = $book; $book['metadata'] = $schema;
restore_current_blog(); $books[] = $book;
}
if ( count( $books ) >= $per_page ) { if ( count( $books ) >= $per_page ) {
break; break;

Loading…
Cancel
Save