Browse Source

Catalog opt-in and faster front page (fixes #65) (#68)

* Widgets not supported
@see pressbooks-aldine/inc/intervention.php

* Catalog page: restore network admin controls (issue #65)

* Connect functionality to get_catalog_data

* Better, strong, faster, ...sort-y-er? (issue #65)

* Fix accessibility issue, handle the old option from networks that have been using Pressbooks Publisher

* Fix eslint failure

* Fix sorting. Fix coding standards.

* Let phpcbf fix things for me.

* Remove odd leftover comment.
pull/70/head
Dac Chartrand 7 years ago committed by Ned Zimmerman
parent
commit
fd0669e842
  1. 13
      404.php
  2. 45
      assets/scripts/catalog-admin.js
  3. 9
      dist/mix-manifest.json
  4. 2
      dist/scripts/aldine.js
  5. 1
      dist/scripts/catalog-admin.js
  6. 2
      dist/scripts/customizer.js
  7. 44
      functions.php
  8. 76
      inc/admin/namespace.php
  9. 108
      inc/helpers/namespace.php
  10. 10
      partials/content-front-page.php
  11. 1
      webpack.mix.js

13
404.php

@ -18,12 +18,10 @@ get_header(); ?>
</header><!-- .page-header -->
<div class="page-content">
<p><?php esc_html_e( 'It looks like nothing was found at this location. Maybe try one of the links below or a search?', 'pressbooks-aldine' ); ?></p>
<p><?php esc_html_e( 'It looks like nothing was found at this location. Maybe try a search?', 'pressbooks-aldine' ); ?></p>
<?php
get_search_form();
the_widget( 'WP_Widget_Recent_Posts' );
?>
<div class="widget widget_categories">
@ -41,15 +39,6 @@ get_header(); ?>
</ul>
</div><!-- .widget -->
<?php
/* translators: %1$s: smiley */
$archive_content = '<p>' . sprintf( esc_html__( 'Try looking in the monthly archives. %1$s', 'pressbooks-aldine' ), convert_smilies( ':)' ) ) . '</p>';
the_widget( 'WP_Widget_Archives', 'dropdown=1', "after_title=</h2>$archive_content" );
the_widget( 'WP_Widget_Tag_Cloud' );
?>
</div><!-- .page-content -->
</section><!-- .error-404 -->

45
assets/scripts/catalog-admin.js

@ -0,0 +1,45 @@
/* global ajaxurl, PB_Aldine_Admin */
( function ( $ ) {
$( document ).ready( function () {
$( '.wrap' ).on( 'click', '.notice-dismiss', function () {
$( this ).parent( '#message' ).fadeOut( 500, function () {
$( this ).remove();
} );
} );
$( 'input.in-catalog' ).on( 'change', function () {
let book_id = $( this ).parent( 'td' ).siblings( 'th' ).children( 'input' ).val();
let in_catalog = $( this ).prop( 'checked' );
$.ajax( {
url: ajaxurl,
type: 'POST',
data: {
action: 'pressbooks_aldine_update_catalog',
book_id: book_id,
in_catalog: in_catalog,
_ajax_nonce: PB_Aldine_Admin.aldineAdminNonce,
},
success: function (){
if ( $( '#message' ).length < 1 ) {
$( '<div id="message" class="updated notice is-dismissible">' ).html( '<p><strong>'+PB_Aldine_Admin.catalog_updated+'</strong></p><button type="button" class="notice-dismiss"><span class="screen-reader-text">'+PB_Aldine_Admin.dismiss_notice+'</span></button>' ).hide().insertAfter( '.wrap h1' ).fadeIn( 500 );
} else {
$( '#message' ).fadeOut( 500, function () {
$( this ).remove();
$( '<div id="message" class="updated notice is-dismissible">' ).html( '<p><strong>'+PB_Aldine_Admin.catalog_updated+'</strong></p><button type="button" class="notice-dismiss"><span class="screen-reader-text">'+PB_Aldine_Admin.dismiss_notice+'</span></button>' ).hide().insertAfter( '.wrap h1' ).fadeIn( 500 );
} );
}
},
error: function ( jqXHR, textStatus, errorThrown ) {
if ( $( '#message' ).length < 1 ) {
$( '<div id="message" class="error notice is-dismissible">' ).html( '<p><strong>'+PB_Aldine_Admin.catalog_not_updated+'</strong></p><button type="button" class="notice-dismiss"><span class="screen-reader-text">'+PB_Aldine_Admin.dismiss_notice+'</span></button>' ).hide().insertAfter( '.wrap h1' ).fadeIn( 500 );
} else {
$( '#message' ).fadeOut( 500, function () {
$( this ).remove();
$( '<div id="message" class="error notice is-dismissible">' ).html( '<p><strong>'+PB_Aldine_Admin.catalog_not_updated+'</strong></p><button type="button" class="notice-dismiss"><span class="screen-reader-text">'+PB_Aldine_Admin.dismiss_notice+'</span></button>' ).hide().insertAfter( '.wrap h1' ).fadeIn( 500 );
} );
}
},
} );
} );
} );
} )( jQuery );

9
dist/mix-manifest.json vendored

@ -1,6 +1,7 @@
{
"/scripts/aldine.js": "/scripts/aldine.js?id=a166171314fdc227903c",
"/styles/aldine.css": "/styles/aldine.css?id=1210a93b205df58a9afb",
"/styles/editor.css": "/styles/editor.css?id=d96762ab5918cde0c2e1",
"/scripts/customizer.js": "/scripts/customizer.js?id=decf119933e3b2d4b210"
"/scripts/aldine.js": "/scripts/aldine.js?id=1f58027f7de9942caf57",
"/styles/aldine.css": "/styles/aldine.css?id=00b0c10d93f464c01cc5",
"/styles/editor.css": "/styles/editor.css?id=10e3f4b144847aa8d75e",
"/scripts/customizer.js": "/scripts/customizer.js?id=1b0d3cfc1d85f460af53",
"/scripts/catalog-admin.js": "/scripts/catalog-admin.js?id=d25f1240496bd3607338"
}

2
dist/scripts/aldine.js vendored

File diff suppressed because one or more lines are too long

1
dist/scripts/catalog-admin.js vendored

File diff suppressed because one or more lines are too long

2
dist/scripts/customizer.js vendored

File diff suppressed because one or more lines are too long

44
functions.php

@ -25,6 +25,7 @@ if ( ! class_exists( 'PressbooksMix\\Assets' ) ) {
$includes = [
'actions',
'activation',
'admin',
'customizer',
'filters',
'helpers',
@ -36,21 +37,28 @@ foreach ( $includes as $include ) {
}
require get_template_directory() . '/inc/intervention.php';
add_action( 'after_switch_theme', '\\Aldine\\Activation\\create_default_content', 10 );
add_action( 'after_switch_theme', '\\Aldine\\Activation\\create_menus', 11 );
add_action( 'after_switch_theme', '\\Aldine\\Activation\\assign_menus', 12 );
add_action( 'admin_bar_init', '\\Aldine\\Actions\\remove_admin_bar_callback' );
add_action( 'after_setup_theme', '\\Aldine\\Actions\\setup' );
add_action( 'after_setup_theme', '\\Aldine\\Actions\\content_width', 0 );
add_action( 'wp_head', '\\Aldine\\Actions\\output_custom_colors' );
add_action( 'init', '\\Aldine\\Actions\\add_editor_styles' );
add_filter( 'mce_buttons', '\\Aldine\\Filters\\add_style_select' );
add_filter( 'tiny_mce_before_init', '\\Aldine\\Filters\\add_blocks' );
add_filter( 'body_class', '\\Aldine\\Filters\\body_classes' );
add_filter( 'excerpt_more', '\\Aldine\\Filters\\excerpt_more' );
add_filter( 'query_vars', '\\Aldine\\Filters\\register_query_vars' );
add_filter( 'wp_nav_menu_items', '\\Aldine\\Filters\\adjust_menu', 10, 2 );
add_action( 'widgets_init', '\\Aldine\\Actions\\widgets_init' );
add_action( 'wp_enqueue_scripts', '\\Aldine\\Actions\\enqueue_assets' );
add_action( 'customize_register', '\\Aldine\\Customizer\\customize_register' );
add_action( 'customize_preview_init', '\\Aldine\\Customizer\\customize_preview_js' );
add_action( 'after_switch_theme', '\Aldine\Activation\create_default_content', 10 );
add_action( 'after_switch_theme', '\Aldine\Activation\create_menus', 11 );
add_action( 'after_switch_theme', '\Aldine\Activation\assign_menus', 12 );
add_action( 'admin_bar_init', '\Aldine\Actions\remove_admin_bar_callback' );
add_action( 'after_setup_theme', '\Aldine\Actions\setup' );
add_action( 'after_setup_theme', '\Aldine\Actions\content_width', 0 );
add_action( 'wp_head', '\Aldine\Actions\output_custom_colors' );
add_action( 'init', '\Aldine\Actions\add_editor_styles' );
add_filter( 'mce_buttons', '\Aldine\Filters\add_style_select' );
add_filter( 'tiny_mce_before_init', '\Aldine\Filters\add_blocks' );
add_filter( 'body_class', '\Aldine\Filters\body_classes' );
add_filter( 'excerpt_more', '\Aldine\Filters\excerpt_more' );
add_filter( 'query_vars', '\Aldine\Filters\register_query_vars' );
add_filter( 'wp_nav_menu_items', '\Aldine\Filters\adjust_menu', 10, 2 );
add_action( 'widgets_init', '\Aldine\Actions\widgets_init' );
add_action( 'wp_enqueue_scripts', '\Aldine\Actions\enqueue_assets' );
add_action( 'customize_register', '\Aldine\Customizer\customize_register' );
add_action( 'customize_preview_init', '\Aldine\Customizer\customize_preview_js' );
// Catalog page: Network admin controls
add_action( 'admin_enqueue_scripts', '\Aldine\Admin\admin_scripts' );
add_action( 'wp_ajax_pressbooks_aldine_update_catalog', '\Aldine\Admin\update_catalog' );
add_filter( 'wpmu_blogs_columns', '\Aldine\Admin\catalog_columns' );
add_action( 'manage_blogs_custom_column', '\Aldine\Admin\catalog_column', 1, 3 );
add_action( 'manage_sites_custom_column', '\Aldine\Admin\catalog_column', 1, 3 );

76
inc/admin/namespace.php

@ -0,0 +1,76 @@
<?php
/**
* @package Aldine
*/
namespace Aldine\Admin;
use PressbooksMix\Assets;
/**
* Uses old option to provide a simpler upgrade path from pressbooks-publisher theme
*/
const BLOG_OPTION = 'pressbooks_publisher_in_catalog';
/**
* @param string $hook
*/
function admin_scripts( $hook ) {
if ( 'sites.php' !== $hook ) {
return;
}
$assets = new Assets( 'pressbooks-aldine', 'theme' );
$assets->setSrcDirectory( 'assets' )->setDistDirectory( 'dist' );
wp_enqueue_script( 'pressbooks-aldine-admin', $assets->getPath( 'scripts/catalog-admin.js' ), [ 'jquery' ] );
wp_localize_script(
'pressbooks-aldine-admin', 'PB_Aldine_Admin', [
'aldineAdminNonce' => wp_create_nonce( 'pressbooks-aldine-admin' ),
'catalog_updated' => __( 'Catalog updated.', 'pressbooks-aldine' ),
'catalog_not_updated' => __( 'Sorry, but your catalog was not updated. Please try again.', 'pressbooks-aldine' ),
'dismiss_notice' => __( 'Dismiss this notice.', 'pressbooks-aldine' ),
]
);
}
/**
*
*/
function update_catalog() {
if ( ! current_user_can( 'manage_network' ) || ! check_ajax_referer( 'pressbooks-aldine-admin' ) ) {
return;
}
$blog_id = absint( $_POST['book_id'] );
$in_catalog = $_POST['in_catalog'];
if ( $in_catalog === 'true' ) {
update_blog_option( $blog_id, \Aldine\Admin\BLOG_OPTION, 1 );
} else {
delete_blog_option( $blog_id, \Aldine\Admin\BLOG_OPTION );
}
}
/**
* @param array $columns
*
* @return array
*/
function catalog_columns( $columns ) {
$columns['in_catalog'] = __( 'In Catalog', 'pressbooks-aldine' );
return $columns;
}
/**
* @param string $column
* @param int $blog_id
*/
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" aria-label="<?php echo esc_attr_x( 'Show in Catalog', 'pressbooks-aldine' ); ?>" <?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 echo esc_attr_x( 'This book is private, so you can&rsquo;t display it in your catalog.', 'pressbooks-aldine' ); ?>"<?php } ?> />
<?php }
}

108
inc/helpers/namespace.php

@ -7,49 +7,87 @@
namespace Aldine\Helpers;
use Pressbooks\Licensing;
use Pressbooks\Book;
use function \Pressbooks\Metadata\book_information_to_schema;
/**
* 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
}
/**
* Filter the WP_Site_Query args for the catalog display.
*
* @since 5.0.0
*/
$args = apply_filters(
'pb_aldine_catalog_query_args',
/** @deprecated */
apply_filters(
'pb_publisher_catalog_query_args',
[
'public' => '1',
'network_id' => get_network()->site_id,
]
)
);
/** @var \WP_Site $site */
$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_in_catalog[] = $site;
}
return [ 'pages' => $pages, 'books' => $books ];
restore_current_blog();
}
if ( $orderby === 'latest' ) {
$sites_in_catalog = wp_list_sort( $sites_in_catalog, 'last_updated', 'DESC' );
} else {
return [ 'pages' => 0, 'books' => [] ];
$sites_in_catalog = wp_list_sort( $sites_in_catalog, 'pb_title', 'ASC' );
}
$total_pages = ceil( count( $sites_in_catalog ) / $per_page );
$offset = ( $page - 1 ) * $per_page;
$books = [];
foreach ( $sites_in_catalog as $i => $site ) {
if ( $i < $offset ) {
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();
if ( count( $books ) >= $per_page ) {
break;
}
}
return [ 'pages' => $total_pages, 'books' => $books ];
}
/**
* Get licenses for catalog display.
*
@ -70,6 +108,7 @@ function get_catalog_licenses() {
* Get licenses currently in use.
*
* @param array $catalog_data
*
* @return array
*/
function get_available_licenses( $catalog_data ) {
@ -90,6 +129,7 @@ function get_available_licenses( $catalog_data ) {
* Get subjects currently in use.
*
* @param array $catalog_data
*
* @return array
*/
function get_available_subjects( $catalog_data ) {
@ -107,6 +147,7 @@ function get_available_subjects( $catalog_data ) {
* Return the default (non-page) menu items.
*
* @param string $items
*
* @return string $items
*/
function get_default_menu( $items = '' ) {
@ -151,9 +192,10 @@ function get_default_menu( $items = '' ) {
);
}
/* @codingStandardsIgnoreStart $items .= sprintf(
'<li class="header__search js-search"><div class="header__search__form">%s</div></li>',
get_search_form( false )
); @codingStandardsIgnoreEnd */
* '<li class="header__search js-search"><div class="header__search__form">%s</div></li>',
* get_search_form( false )
* ); @codingStandardsIgnoreEnd
*/
return $items;
}
@ -162,6 +204,7 @@ function get_default_menu( $items = '' ) {
* Echo the default menu.
*
* @param string $items
*
* @return null
*/
function default_menu( $args = [], $items = '' ) {
@ -243,6 +286,7 @@ function handle_contact_form_submission() {
* Does a page have page sections?
*
* @param int $post_id The page.
*
* @return bool
*/
function has_sections( $post_id ) {

10
partials/content-front-page.php

@ -15,10 +15,12 @@ use function Aldine\Helpers\get_catalog_data;
$front_page_catalog = get_option( 'pb_front_page_catalog' );
$latest_books_title = get_option( 'pb_front_page_catalog_title', __( 'Our Latest Titles', 'pressbooks-aldine' ) );
$page = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
$catalog_data = get_catalog_data( $page, 3 );
$previous_page = ( $page > 1 ) ? $page - 1 : 0;
$next_page = $page + 1;
if ( get_option( 'pb_front_page_catalog' ) ) {
$page = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
$catalog_data = get_catalog_data( $page, 3, 'latest' );
$previous_page = ( $page > 1 ) ? $page - 1 : 0;
$next_page = $page + 1;
}
?>

1
webpack.mix.js

@ -42,6 +42,7 @@ mix.autoload({ jquery: ['$', 'window.jQuery', 'jQuery'] });
mix
.js(`${assets}/scripts/aldine.js`, `${dist}/scripts`)
.js(`${assets}/scripts/catalog-admin.js`, `${dist}/scripts`)
.js(`${assets}/scripts/customizer.js`, `${dist}/scripts`);
// Assets

Loading…
Cancel
Save