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. 104
      inc/helpers/namespace.php
  10. 4
      partials/content-front-page.php
  11. 1
      webpack.mix.js

13
404.php

@ -18,12 +18,10 @@ get_header(); ?>
</header><!-- .page-header --> </header><!-- .page-header -->
<div class="page-content"> <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 <?php
get_search_form(); get_search_form();
the_widget( 'WP_Widget_Recent_Posts' );
?> ?>
<div class="widget widget_categories"> <div class="widget widget_categories">
@ -41,15 +39,6 @@ get_header(); ?>
</ul> </ul>
</div><!-- .widget --> </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 --> </div><!-- .page-content -->
</section><!-- .error-404 --> </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", "/scripts/aldine.js": "/scripts/aldine.js?id=1f58027f7de9942caf57",
"/styles/aldine.css": "/styles/aldine.css?id=1210a93b205df58a9afb", "/styles/aldine.css": "/styles/aldine.css?id=00b0c10d93f464c01cc5",
"/styles/editor.css": "/styles/editor.css?id=d96762ab5918cde0c2e1", "/styles/editor.css": "/styles/editor.css?id=10e3f4b144847aa8d75e",
"/scripts/customizer.js": "/scripts/customizer.js?id=decf119933e3b2d4b210" "/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 = [ $includes = [
'actions', 'actions',
'activation', 'activation',
'admin',
'customizer', 'customizer',
'filters', 'filters',
'helpers', 'helpers',
@ -36,21 +37,28 @@ foreach ( $includes as $include ) {
} }
require get_template_directory() . '/inc/intervention.php'; 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_default_content', 10 );
add_action( 'after_switch_theme', '\\Aldine\\Activation\\create_menus', 11 ); add_action( 'after_switch_theme', '\Aldine\Activation\create_menus', 11 );
add_action( 'after_switch_theme', '\\Aldine\\Activation\\assign_menus', 12 ); add_action( 'after_switch_theme', '\Aldine\Activation\assign_menus', 12 );
add_action( 'admin_bar_init', '\\Aldine\\Actions\\remove_admin_bar_callback' ); 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\setup' );
add_action( 'after_setup_theme', '\\Aldine\\Actions\\content_width', 0 ); add_action( 'after_setup_theme', '\Aldine\Actions\content_width', 0 );
add_action( 'wp_head', '\\Aldine\\Actions\\output_custom_colors' ); add_action( 'wp_head', '\Aldine\Actions\output_custom_colors' );
add_action( 'init', '\\Aldine\\Actions\\add_editor_styles' ); add_action( 'init', '\Aldine\Actions\add_editor_styles' );
add_filter( 'mce_buttons', '\\Aldine\\Filters\\add_style_select' ); add_filter( 'mce_buttons', '\Aldine\Filters\add_style_select' );
add_filter( 'tiny_mce_before_init', '\\Aldine\\Filters\\add_blocks' ); add_filter( 'tiny_mce_before_init', '\Aldine\Filters\add_blocks' );
add_filter( 'body_class', '\\Aldine\\Filters\\body_classes' ); add_filter( 'body_class', '\Aldine\Filters\body_classes' );
add_filter( 'excerpt_more', '\\Aldine\\Filters\\excerpt_more' ); add_filter( 'excerpt_more', '\Aldine\Filters\excerpt_more' );
add_filter( 'query_vars', '\\Aldine\\Filters\\register_query_vars' ); add_filter( 'query_vars', '\Aldine\Filters\register_query_vars' );
add_filter( 'wp_nav_menu_items', '\\Aldine\\Filters\\adjust_menu', 10, 2 ); add_filter( 'wp_nav_menu_items', '\Aldine\Filters\adjust_menu', 10, 2 );
add_action( 'widgets_init', '\\Aldine\\Actions\\widgets_init' ); add_action( 'widgets_init', '\Aldine\Actions\widgets_init' );
add_action( 'wp_enqueue_scripts', '\\Aldine\\Actions\\enqueue_assets' ); add_action( 'wp_enqueue_scripts', '\Aldine\Actions\enqueue_assets' );
add_action( 'customize_register', '\\Aldine\\Customizer\\customize_register' ); add_action( 'customize_register', '\Aldine\Customizer\customize_register' );
add_action( 'customize_preview_init', '\\Aldine\\Customizer\\customize_preview_js' ); 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 }
}

104
inc/helpers/namespace.php

@ -7,49 +7,87 @@
namespace Aldine\Helpers; 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 $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,
]); /**
$response = rest_do_request( $request ); * Filter the WP_Site_Query args for the catalog display.
$pages = $response->headers['X-WP-TotalPages']; *
$data = rest_get_server()->response_to_data( $response, true ); * @since 5.0.0
$books = []; */
foreach ( $data as $key => $book ) { $args = apply_filters(
$book['title'] = $book['metadata']['name']; 'pb_aldine_catalog_query_args',
$book['date-published'] = ( isset( $book['metadata']['datePublished'] ) ) ? /** @deprecated */
$book['metadata']['datePublished'] : apply_filters(
''; 'pb_publisher_catalog_query_args',
$book['subject'] = ( isset( $book['metadata']['about'][0] ) ) [
? $book['metadata']['about'][0]['identifier'] 'public' => '1',
: ''; 'network_id' => get_network()->site_id,
$books[] = $book; ]
)
);
/** @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;
}
restore_current_blog();
} }
if ( $orderby === 'latest' ) { if ( $orderby === 'latest' ) {
$books = wp_list_sort( $books, $orderby, 'desc' ); $sites_in_catalog = wp_list_sort( $sites_in_catalog, 'last_updated', 'DESC' );
} else { } else {
$books = wp_list_sort( $books, $orderby ); $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' => $pages, 'books' => $books ];
} else {
return [ 'pages' => 0, 'books' => [] ];
} }
return [ 'pages' => $total_pages, 'books' => $books ];
} }
/** /**
* Get licenses for catalog display. * Get licenses for catalog display.
* *
@ -70,6 +108,7 @@ function get_catalog_licenses() {
* Get licenses currently in use. * Get licenses currently in use.
* *
* @param array $catalog_data * @param array $catalog_data
*
* @return array * @return array
*/ */
function get_available_licenses( $catalog_data ) { function get_available_licenses( $catalog_data ) {
@ -90,6 +129,7 @@ function get_available_licenses( $catalog_data ) {
* Get subjects currently in use. * Get subjects currently in use.
* *
* @param array $catalog_data * @param array $catalog_data
*
* @return array * @return array
*/ */
function get_available_subjects( $catalog_data ) { function get_available_subjects( $catalog_data ) {
@ -107,6 +147,7 @@ function get_available_subjects( $catalog_data ) {
* Return the default (non-page) menu items. * Return the default (non-page) menu items.
* *
* @param string $items * @param string $items
*
* @return string $items * @return string $items
*/ */
function get_default_menu( $items = '' ) { function get_default_menu( $items = '' ) {
@ -151,9 +192,10 @@ function get_default_menu( $items = '' ) {
); );
} }
/* @codingStandardsIgnoreStart $items .= sprintf( /* @codingStandardsIgnoreStart $items .= sprintf(
'<li class="header__search js-search"><div class="header__search__form">%s</div></li>', * '<li class="header__search js-search"><div class="header__search__form">%s</div></li>',
get_search_form( false ) * get_search_form( false )
); @codingStandardsIgnoreEnd */ * ); @codingStandardsIgnoreEnd
*/
return $items; return $items;
} }
@ -162,6 +204,7 @@ function get_default_menu( $items = '' ) {
* Echo the default menu. * Echo the default menu.
* *
* @param string $items * @param string $items
*
* @return null * @return null
*/ */
function default_menu( $args = [], $items = '' ) { function default_menu( $args = [], $items = '' ) {
@ -243,6 +286,7 @@ function handle_contact_form_submission() {
* Does a page have page sections? * Does a page have page sections?
* *
* @param int $post_id The page. * @param int $post_id The page.
*
* @return bool * @return bool
*/ */
function has_sections( $post_id ) { function has_sections( $post_id ) {

4
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' ); $front_page_catalog = get_option( 'pb_front_page_catalog' );
$latest_books_title = get_option( 'pb_front_page_catalog_title', __( 'Our Latest Titles', 'pressbooks-aldine' ) ); $latest_books_title = get_option( 'pb_front_page_catalog_title', __( 'Our Latest Titles', 'pressbooks-aldine' ) );
if ( get_option( 'pb_front_page_catalog' ) ) {
$page = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1; $page = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
$catalog_data = get_catalog_data( $page, 3 ); $catalog_data = get_catalog_data( $page, 3, 'latest' );
$previous_page = ( $page > 1 ) ? $page - 1 : 0; $previous_page = ( $page > 1 ) ? $page - 1 : 0;
$next_page = $page + 1; $next_page = $page + 1;
}
?> ?>

1
webpack.mix.js

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

Loading…
Cancel
Save