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; use PressbooksMix\Assets;
const BLOG_OPTION = 'pressbooks_aldine_in_catalog';
/** /**
* @param string $hook * @param string $hook
*/ */
@ -41,9 +43,9 @@ function update_catalog() {
$in_catalog = $_POST['in_catalog']; $in_catalog = $_POST['in_catalog'];
if ( $in_catalog == 'true' ) { if ( $in_catalog == 'true' ) {
update_blog_option( $blog_id, 'in_catalog', 1 ); update_blog_option( $blog_id, \Aldine\Admin\BLOG_OPTION, 1 );
} else { } 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 ) { function catalog_column( $column, $blog_id ) {
if ( 'in_catalog' == $column && ! is_main_site( $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 } ?> /> 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 } <?php }

62
inc/helpers/namespace.php

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

Loading…
Cancel
Save