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. 21
      inc/helpers/namespace.php

3
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 );
}
}

21
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_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;
restore_current_blog();
}
if ( count( $books ) >= $per_page ) {
break;

Loading…
Cancel
Save