diff --git a/additionalPosts-biblio.php b/additionalPosts-biblio.php index 9968347..aedbd75 100644 --- a/additionalPosts-biblio.php +++ b/additionalPosts-biblio.php @@ -62,7 +62,7 @@ - + diff --git a/additionalPosts-cat.php b/additionalPosts-cat.php index 45669c8..ae96224 100644 --- a/additionalPosts-cat.php +++ b/additionalPosts-cat.php @@ -68,9 +68,7 @@ - + have_posts() ) : ?> @@ -86,7 +84,7 @@ - + diff --git a/additionalPosts-search.php b/additionalPosts-search.php index 72a094c..f6fc065 100644 --- a/additionalPosts-search.php +++ b/additionalPosts-search.php @@ -55,7 +55,7 @@ function set_search( $q ) { if ( 'search' == $query_split[0] ) { $search_args['s'] = urldecode( $query_split[1] ); } -} // End foreach(). +} $the_query = new WP_Query( $search_args ); // The set_search() function is defined above. diff --git a/archive.php b/archive.php index 813809b..140ce50 100644 --- a/archive.php +++ b/archive.php @@ -20,15 +20,12 @@ get_header(); $date = DateTime::createFromFormat( 'Ymd', get_field( 'event_date' ) ); +get_template_part( 'inc/sub-header' ); +if ( (get_post_type( get_the_ID() ) === 'bibliotech') || (cat_is_ancestor_of( 73, $cat ) or is_category( 73 )) ) { + get_template_part( 'inc/bib-header' ); +} ?> - - - - - -
@@ -36,7 +33,7 @@
-
+
@@ -18,7 +16,7 @@
-
+
' . 'Category: ' . '' . single_cat_title( '', false ) . '' . '' ); @@ -33,7 +31,7 @@ - + diff --git a/codesniffer.ruleset.xml b/codesniffer.ruleset.xml index b05691f..83ab96f 100644 --- a/codesniffer.ruleset.xml +++ b/codesniffer.ruleset.xml @@ -13,6 +13,8 @@ + + diff --git a/functions.php b/functions.php index 56fc46b..abef27d 100644 --- a/functions.php +++ b/functions.php @@ -113,6 +113,7 @@ function mitlibnews_register_news_posts() { 'labels' => $labelsFeatures, 'public' => true, 'menu_position' => 5, + 'show_in_rest' => true, 'supports' => array( 'title' ), 'taxonomies' => array( 'category' ), @@ -157,6 +158,7 @@ function theme_apto_object_taxonomies( $object_taxonomies, $post_type ) { 'labels' => $labelsFeatures, 'public' => true, 'menu_position' => 5, + 'show_in_rest' => true, 'supports' => $supports_default, 'taxonomies' => array( 'category' ), ); @@ -357,6 +359,7 @@ function biblio_taxonomy() { 'show_ui' => true, 'show_admin_column' => true, 'show_in_nav_menus' => true, + 'show_in_rest' => true, 'show_tagcloud' => true, ); register_taxonomy( 'bibliotech_issues', array( 'bibliotech' ), $args ); @@ -366,7 +369,7 @@ function biblio_taxonomy() { // Hook into the 'init' action. add_action( 'init', 'biblio_taxonomy', 0 ); -} // End if(). +} // End bibliotechs custom taxonomy. /** * Registers news sidebar @@ -398,3 +401,187 @@ function SearchFilter( $query ) { return $query; } add_filter( 'pre_get_posts','SearchFilter' ); + + +/** + * Custom API endpoints + * + * @param WP_REST_Request $request Full data about the request. + */ +function mitlibnews_cards( WP_REST_Request $request ) { + + $args = array(); + $response = array(); + + // Get passed parameters for building the right query. + $params = $request->get_params(); + + // Build query type. + $categories = $params['categories']; + + // Posts per page needs to be an integer. + $args['posts_per_page'] = mitlibnews_cardargs_postsperpage( $params['filter']['posts_per_page'] ); + + // Only certain post types are recognized. + $args['post_type'] = mitlibnews_cardargs_posttype( $params['type'] ); + + // If requested, filter by author. + $args['author'] = mitlibnews_cardargs_author( $params['filter'] ); + + // If requested, filter by a single category. + if ( array_key_exists( 'categories', $params ) && is_array( $categories ) && is_int( (int) $params['categories'][0] ) ) { + $args['cat'] = (int) $params['categories'][0]; + } + + // If requested, filter by a single tag. + if ( array_key_exists( 'issue', $params ) ) { + $args['bibliotech_issues'] = (string) $params['issue']; + } + + // If requested, filter by a single tag. + if ( array_key_exists( 's', $params ) ) { + $args['s'] = (string) $params['s']; + } + + // If requested, change sort order. + $args['orderby'] = mitlibnews_cardargs_orderby( $params['filter'] ); + + // If requested, filter by custom field. + if ( array_key_exists( 'meta_query', $params['filter'] ) ) { + $args['meta_query'] = mitlibnews_cardargs_metaquery( $params['filter']['meta_query'] ); + } + + // Build the query. + $args['order'] = 'DESC'; + + // Execute the query. + $posts = new WP_Query( $args ); + + // Assemble the response. + $controller = new WP_REST_Posts_Controller( 'post' ); + if ( $posts->have_posts() ) { + while ( $posts->have_posts() ) { + $posts->the_post(); + $post = $posts->post; + $data = $controller->prepare_item_for_response( $post, $request ); + $response[] = $controller->prepare_response_for_collection( $data ); + } + wp_reset_postdata(); + } + + // Return the response. + return new WP_REST_Response( $response, 200 ); + +} +add_action( 'rest_api_init', function() { + register_rest_route( 'mitlibnews/v1', '/cards', array( + 'methods' => 'GET', + 'callback' => 'mitlibnews_cards', + )); +}); + +/** + * Assemble API query: author + * + * @param array $input Contents of $params['filter']. + */ +function mitlibnews_cardargs_author( $input ) { + if ( array_key_exists( 'author', $input ) && is_int( (int) $input['author'] ) ) { + return (int) $input['author']; + } + return ''; +} + +/** + * Assemble API query: orderby + * + * @param array $input Contents of $params['filter']. + */ +function mitlibnews_cardargs_orderby( $input ) { + $orderby = 'post_date'; + if ( array_key_exists( 'orderby', $input ) ) { + $orderby = (string) $input['orderby']; + } + return $orderby; +} + + +/** + * Assemble API query: posts_per_page + * + * @param int $input Contents of $params['filter']['posts_per_page']. + */ +function mitlibnews_cardargs_postsperpage( $input ) { + $posts_per_page = 9; + if ( is_int( (int) $input ) ) { + $posts_per_page = (int) $input; + } + return $posts_per_page; +} + +/** + * Assemble API query: post_type + * + * Only three values are accepted for post_type queries + * + * @param int $input Contents of $params['type']. + */ +function mitlibnews_cardargs_posttype( $input ) { + $post_type = array(); + if ( is_array( $input ) ) { + if ( in_array( 'bibliotech', $input, true ) ) { + $post_type[] = 'bibliotech'; + } + if ( in_array( 'post', $input, true ) ) { + $post_type[] = 'post'; + } + if ( in_array( 'spotlights', $input, true ) ) { + $post_type[] = 'spotlights'; + } + } + return $post_type; +} + +/** + * Assemble API query: meta_query array + * + * @param int $input Contents of $params['filter']. + */ +function mitlibnews_cardargs_metaquery( $input ) { + $meta_query = array(); + if ( 'is_event' === $input[0]['key'] ) { + // We currently only support meta_queries based on the is_event custom field. + if ( 'false' === $input[0]['value'] ) { + // The query is for items which aren't events. + $meta_query[] = mitlibnews_cardargs_metaquery_item( 'is_event', '!=', '1', 'NUMERIC' ); + } elseif ( 'true' === $input[0]['value'] ) { + // The query is for events, but past or future? + $meta_query[] = mitlibnews_cardargs_metaquery_item( 'is_event', '=', '1', 'NUMERIC' ); + if ( 'future' === $input[0]['type'] ) { + // Query for future events. + $meta_query[] = mitlibnews_cardargs_metaquery_item( 'event_date', '>=', date( 'Y-m-d' ), 'DATE' ); + } elseif ( 'past' === $input[0]['type'] ) { + // Query for past events. + $meta_query[] = mitlibnews_cardargs_metaquery_item( 'event_date', '<', date( 'Y-m-d' ), 'DATE' ); + } + } + } + return $meta_query; +} + +/** + * Assemble API query: meta_query item + * + * @param string $key The variable being tested. + * @param string $compare The comparision being performed. + * @param string $value The value being tested for. + * @param string $type The type of comparison being performed. + */ +function mitlibnews_cardargs_metaquery_item( $key, $compare, $value, $type ) { + return array( + 'key' => $key, + 'value' => $value, + 'compare' => $compare, + 'type' => $type, + ); +} diff --git a/inc/events.php b/inc/events.php index b30dff7..c76f902 100644 --- a/inc/events.php +++ b/inc/events.php @@ -48,5 +48,5 @@
- + diff --git a/index.php b/index.php index 954a63b..3eaa9e5 100644 --- a/index.php +++ b/index.php @@ -89,7 +89,7 @@
-
+
6 ) { get_template_part( 'inc/more-posts' ); -} // End if(). +} ?>
diff --git a/js/src/mitlibnews.loader.js b/js/src/mitlibnews.loader.js index 897f431..c55c8af 100644 --- a/js/src/mitlibnews.loader.js +++ b/js/src/mitlibnews.loader.js @@ -42,11 +42,15 @@ Loader.prototype = { // Initialization initialize : function() { // Look up specified post container in DOM + console.log('Looking for ' + this.getContainer() ); this.postcontainer = document.getElementById( this.getContainer() ); // If no post container is found, then we quit and do nothing further. if( !this.postcontainer ) { + console.log('Post container not found...'); return false; + } else { + console.log('Post container found. Continuing...'); } // Read data attributes @@ -68,12 +72,53 @@ Loader.prototype = { var filter = { 'posts_per_page': this.getPagesize(), }; + var type = ['post', 'bibliotech', 'spotlights']; // Context-specific values if ( this.getPostcontent() === 'author' ) { filter.author = this.postcontainer.dataset.postauthor; + // Author indexes don't show Spotlights + type = ['post', 'bibliotech']; + } else if ( this.getPostcontent() === 'bibliotech' ) { + // Only return bibliotech content + type = ['bibliotech']; + } else if ( this.getPostcontent() === 'category' ) { + query.categories = [this.postcontainer.dataset.postcategory]; + } else if ( this.getPostcontent() === 'futureevents' ) { + // Only return post content + type = ['post']; + filter.meta_query = [{ + 'key': 'is_event', + 'value': true, + 'type': 'future', + }]; + } else if ( this.getPostcontent() === 'issue' ) { + query.issue = this.postcontainer.dataset.postissue; + type = ['bibliotech']; + } else if ( this.getPostcontent() === 'news' ) { + // Only return post content + type = ['post']; + filter.meta_query = [{ + 'key': 'is_event', + 'value': false, + }]; + } else if ( this.getPostcontent() === 'pastevents' ) { + // Only return post content + type = ['post']; + filter.meta_query = [{ + 'key': 'is_event', + 'value': true, + 'type': 'past', + }]; + } else if ( this.getPostcontent() === 'related' ) { + // Related queries are sorted randomly + filter.orderby = 'rand'; + query.categories = [this.postcontainer.dataset.postcategory]; + } else if ( this.getPostcontent() === 'search' ) { + query.search = this.postcontainer.dataset.search; } // Assemble pieces into query object query.filter = filter; + query.type = type; return query; }, @@ -108,12 +153,13 @@ Loader.prototype = { // Query the API jQuery.ajax({ - url: '/news/wp-json/posts', + url: '/news/wp-json/mitlibnews/v1/cards', data: query, dataType: 'json', type: 'GET', success: function(data) { jQuery.each(data, function( index, value ) { + console.log('\n' + value.type + '\n' + value.title.rendered ); console.log(value); }); }, diff --git a/js/tests/mitlibnews.loader.spec.js b/js/tests/mitlibnews.loader.spec.js index 4ad55f3..6fb5bee 100644 --- a/js/tests/mitlibnews.loader.spec.js +++ b/js/tests/mitlibnews.loader.spec.js @@ -49,7 +49,8 @@ describe("Loader test suite", function() { page: 1, filter: { posts_per_page: 9, - } + }, + type: ['post', 'bibliotech', 'spotlights'] } expect( test.buildQuery() ).toEqual(defaultQuery); }); diff --git a/page-biblio-archive.php b/page-biblio-archive.php index 2acb03b..9c8f6ea 100644 --- a/page-biblio-archive.php +++ b/page-biblio-archive.php @@ -59,7 +59,7 @@ ?>

diff --git a/page-bibliotech.php b/page-bibliotech.php index a101bd7..f428b2d 100644 --- a/page-bibliotech.php +++ b/page-bibliotech.php @@ -50,7 +50,7 @@ - + @@ -58,7 +58,7 @@
-
+
posts; ?> -

Upcoming classes & events

+
0 ) { $i = -1; @@ -123,7 +123,7 @@

Past classes & events

-
+
0 ) { $i = -1; @@ -146,9 +146,8 @@ - diff --git a/page-news.php b/page-news.php index 4ef8412..b69a313 100644 --- a/page-news.php +++ b/page-news.php @@ -22,7 +22,7 @@
-
+

News

diff --git a/search.php b/search.php index ed4ea60..6c2aa59 100644 --- a/search.php +++ b/search.php @@ -15,9 +15,9 @@
-
+ +
- diff --git a/single.php b/single.php index bfb51f0..bb472a1 100644 --- a/single.php +++ b/single.php @@ -109,7 +109,7 @@
- + @@ -171,20 +171,21 @@ $currentPost = get_the_ID(); -$myCatId = $category[ $r ]->cat_ID; + $category_id = $category[ $r ]->cat_ID; $args = array( 'post_type' => array( 'post', 'bibliotech', 'spotlights' ), - 'cat' => $myCatId, + 'cat' => $category_id, 'posts_per_page' => '3', 'order' => 'DESC', 'orderby' => 'date', 'post__not_in' => array( $currentPost ), ); - ?> + +?> -
+
- + @@ -261,7 +261,7 @@

- +