Browse Source

Connect functionality to get_catalog_data

pull/68/head
Dac Chartrand 8 years ago
parent
commit
0211a93fe4
  1. 8
      inc/admin/namespace.php
  2. 62
      inc/helpers/namespace.php

8
inc/admin/namespace.php

@ -7,6 +7,8 @@ namespace Aldine\Admin;
use PressbooksMix\Assets;
const BLOG_OPTION = 'pressbooks_aldine_in_catalog';
/**
* @param string $hook
*/
@ -41,9 +43,9 @@ function update_catalog() {
$in_catalog = $_POST['in_catalog'];
if ( $in_catalog == 'true' ) {
update_blog_option( $blog_id, 'in_catalog', 1 );
update_blog_option( $blog_id, \Aldine\Admin\BLOG_OPTION, 1 );
} else {
delete_blog_option( $blog_id, 'in_catalog' );
delete_blog_option( $blog_id, \Aldine\Admin\BLOG_OPTION );
}
}
@ -64,7 +66,7 @@ function catalog_columns( $columns ) {
function catalog_column( $column, $blog_id ) {
if ( 'in_catalog' == $column && ! is_main_site( $blog_id ) ) { ?>
<input class="in-catalog" type="checkbox" name="in_catalog" value="1" <?php checked( get_blog_option( $blog_id, 'in_catalog' ), 1 ); ?> <?php
<input class="in-catalog" type="checkbox" name="in_catalog" value="1" <?php checked( get_blog_option( $blog_id, \Aldine\Admin\BLOG_OPTION ), 1 ); ?> <?php
if ( ! get_blog_option( $blog_id, 'blog_public' ) ) { ?>disabled="disabled" title="<?php _e( 'This book is private, so you can&rsquo;t display it in your catalog.', 'pressbooks-aldine' ); ?>"<?php } ?> />
<?php }

62
inc/helpers/namespace.php

@ -7,47 +7,49 @@
namespace Aldine\Helpers;
use Pressbooks\Licensing;
/**
* Get catalog data.
*
* @param int $page
* @param int $per_page
* @param string $orderby
* @param string $license
* @param string $subject
*
* @return array
*/
function get_catalog_data( $page = 1, $per_page = 10, $orderby = 'title', $license = '', $subject = '' ) {
if ( defined( 'PB_PLUGIN_VERSION' ) ) {
$request = new \WP_REST_Request( 'GET', '/pressbooks/v2/books' );
$request->set_query_params([
'page' => $page,
'per_page' => $per_page,
]);
$response = rest_do_request( $request );
$pages = $response->headers['X-WP-TotalPages'];
$data = rest_get_server()->response_to_data( $response, true );
$books = [];
foreach ( $data as $key => $book ) {
$book['title'] = $book['metadata']['name'];
$book['date-published'] = ( isset( $book['metadata']['datePublished'] ) ) ?
$book['metadata']['datePublished'] :
'';
$book['subject'] = ( isset( $book['metadata']['about'][0] ) )
? $book['metadata']['about'][0]['identifier']
: '';
$books[] = $book;
}
if ( $orderby === 'latest' ) {
$books = wp_list_sort( $books, $orderby, 'desc' );
} else {
$books = wp_list_sort( $books, $orderby );
if ( ! defined( 'PB_PLUGIN_VERSION' ) ) {
return [ 'pages' => 0, 'books' => [] ]; // Bail
}
$request = new \WP_REST_Request( 'GET', '/pressbooks/v2/books' );
$request->set_query_params(
[
'page' => $page,
'per_page' => $per_page,
]
);
$response = rest_do_request( $request );
$pages = $response->headers['X-WP-TotalPages'];
$data = rest_get_server()->response_to_data( $response, true );
$books = [];
foreach ( $data as $key => $book ) {
if ( ! get_blog_option( $book['id'], \Aldine\Admin\BLOG_OPTION ) ) {
continue; // Skip
}
return [ 'pages' => $pages, 'books' => $books ];
$book['title'] = $book['metadata']['name'];
$book['date-published'] = $book['metadata']['datePublished'] ?? '';
$book['subject'] = $book['metadata']['about'][0]['identifier'] ?? '';
$books[] = $book;
}
if ( $orderby === 'latest' ) {
$books = wp_list_sort( $books, $orderby, 'desc' );
} else {
return [ 'pages' => 0, 'books' => [] ];
$books = wp_list_sort( $books, $orderby );
}
return [ 'pages' => $pages, 'books' => $books ];
}
/**

Loading…
Cancel
Save