Skip to content

Commit

Permalink
feat: use post categories and tags for all listing post types (#39)
Browse files Browse the repository at this point in the history
* feat: use post categories and tags for all listing post types

* fix: undo patterns post type changes (for another PR)

* chore: remove unneeded admin page highlighting filter

BREAKING CHANGE: This feature will deprecate existing custom taxonomies, so any existing terms for those taxonomies will be lost.

To fix, we can convert terms from the deprecated taxonomies to standard post categories/tags via a migration script.


Fixes #32.
  • Loading branch information
dkoo authored Feb 26, 2021
1 parent 751c138 commit f223053
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 103 deletions.
14 changes: 7 additions & 7 deletions includes/class-newspack-listings-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,29 +146,29 @@ public static function build_listings_query( $query, $args = [] ) {
}
if ( ! empty( $query['categories'] ) ) {
$args['tax_query'][] = [
'taxonomy' => Core::NEWSPACK_LISTINGS_CAT,
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $query['categories'],
];
}
if ( ! empty( $query['tags'] ) ) {
$args['tax_query'][] = [
'taxonomy' => Core::NEWSPACK_LISTINGS_TAG,
'taxonomy' => 'post_tag',
'field' => 'term_id',
'terms' => $query['tags'],
];
}
if ( ! empty( $query['categoryExclusions'] ) ) {
$args['tax_query'][] = [
'taxonomy' => Core::NEWSPACK_LISTINGS_CAT,
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $query['categoryExclusions'],
'operator' => 'NOT IN',
];
}
if ( ! empty( $query['tagExclusions'] ) ) {
$args['tax_query'][] = [
'taxonomy' => Core::NEWSPACK_LISTINGS_TAG,
'taxonomy' => 'post_tag',
'field' => 'term_id',
'terms' => $query['tagExclusions'],
'operator' => 'NOT IN',
Expand Down Expand Up @@ -259,12 +259,12 @@ function( $post ) use ( $attributes, $fields, $is_amp, $next_page, $query ) {

// If $fields includes category, get the post categories.
if ( in_array( 'category', $fields ) ) {
$item['category'] = get_the_terms( $post->ID, Core::NEWSPACK_LISTINGS_CAT );
$item['category'] = get_the_terms( $post->ID, 'category' );
}

// If $fields includes tags, get the post tags.
if ( in_array( 'tags', $fields ) ) {
$item['tags'] = get_the_terms( $post->ID, Core::NEWSPACK_LISTINGS_TAG );
$item['tags'] = get_the_terms( $post->ID, 'post_tag' );
}

// If $fields includes author and the post isn't set to hide author, get the post author.
Expand Down Expand Up @@ -340,7 +340,7 @@ public static function get_terms( $request ) {
$params = $request->get_params();

if ( empty( $params['taxonomy'] ) ) {
$params['taxonomy'] = Core::NEWSPACK_LISTINGS_CAT;
$params['taxonomy'] = 'category';
}

$terms = get_terms( $params );
Expand Down
4 changes: 0 additions & 4 deletions includes/class-newspack-listings-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ public static function manage_editor_assets() {
'post_type_label' => get_post_type_object( $post_type )->labels->singular_name,
'post_type' => $post_type,
'post_types' => $post_types,
'taxonomies' => [
'category' => Core::NEWSPACK_LISTINGS_CAT,
'tag' => Core::NEWSPACK_LISTINGS_TAG,
],

// If we don't have ANY listings that can be added to a list yet, alert the editor so we can show messaging.
'no_listings' => 0 === $total_count,
Expand Down
84 changes: 7 additions & 77 deletions includes/class-newspack-listings-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

/**
* Main Core class.
* Sets up CPTs and taxonomies for listings.
* Sets up CPTs for listings.
*/
final class Newspack_Listings_Core {

Expand All @@ -42,12 +42,6 @@ final class Newspack_Listings_Core {

];

/**
* Custom taxonomy slugs for Newspack Listings.
*/
const NEWSPACK_LISTINGS_CAT = 'newspack_lst_cat';
const NEWSPACK_LISTINGS_TAG = 'newspack_lst_tag';

/**
* The single instance of the class.
*
Expand All @@ -73,9 +67,7 @@ public static function instance() {
*/
public function __construct() {
add_action( 'admin_menu', [ __CLASS__, 'add_plugin_page' ] );
add_filter( 'parent_file', [ __CLASS__, 'highlight_taxonomy_menu_items' ] );
add_action( 'init', [ __CLASS__, 'register_post_types' ] );
add_action( 'init', [ __CLASS__, 'register_taxonomies' ] );
add_filter( 'single_template', [ __CLASS__, 'set_default_template' ] );
add_action( 'save_post', [ __CLASS__, 'sync_post_meta' ], 10, 2 );
add_filter( 'newspack_listings_hide_author', [ __CLASS__, 'hide_author' ] );
Expand All @@ -101,16 +93,16 @@ public static function add_plugin_page() {
add_submenu_page(
'newspack-listings',
__( 'Newspack Listings: Categories', 'newspack-listings' ),
__( 'Listing Categories', 'newspack-listings' ),
__( 'Categories', 'newspack-listings' ),
'manage_categories',
'edit-tags.php?taxonomy=' . self::NEWSPACK_LISTINGS_CAT
'edit-tags.php?taxonomy=category'
);
add_submenu_page(
'newspack-listings',
__( 'Newspack Listings: Tags', 'newspack-listings' ),
__( 'Listing Tags', 'newspack-listings' ),
__( 'Tags', 'newspack-listings' ),
'manage_categories',
'edit-tags.php?taxonomy=' . self::NEWSPACK_LISTINGS_TAG
'edit-tags.php?taxonomy=post_tag'
);

// Settings menu link.
Expand All @@ -124,20 +116,6 @@ public static function add_plugin_page() {
);
}

/**
* Hack to highlight category/tag menu items.
* https://deluxeblogtips.com/move-taxonomy-admin-menu/
*
* @param string $parent_file The parent file.
*/
public static function highlight_taxonomy_menu_items( $parent_file ) {
if ( get_current_screen()->taxonomy == self::NEWSPACK_LISTINGS_CAT || get_current_screen()->taxonomy == self::NEWSPACK_LISTINGS_TAG ) {
$parent_file = 'newspack-listings';
}

return $parent_file;
}

/**
* Is the current post a listings post type?
*
Expand Down Expand Up @@ -169,6 +147,7 @@ public static function register_post_types() {
'show_in_rest' => true,
'show_ui' => true,
'supports' => [ 'editor', 'excerpt', 'title', 'author', 'custom-fields', 'thumbnail' ],
'taxonomies' => [ 'category', 'post_tag' ],
];
$post_types_config = [
'event' => [
Expand Down Expand Up @@ -272,54 +251,6 @@ public static function register_post_types() {
}
}

/**
* Register custom taxonomies for Listings CPTs.
*/
public static function register_taxonomies() {
$prefix = Settings::get_settings( 'permalink_prefix' );
$category_args = [
'hierarchical' => true,
'public' => true,
'rewrite' => false, // phpcs:ignore Squiz.PHP.CommentedOutCode.Found [ 'hierarchical' => true, 'slug' => $prefix . '/category' ]
'show_in_menu' => true,
'show_in_rest' => true,
'show_tagcloud' => false,
'show_ui' => true,
];
$tag_args = [
'hierarchical' => false,
'public' => true,
'rewrite' => false, // phpcs:ignore Squiz.PHP.CommentedOutCode.Found [ 'slug' => $prefix . '/tag' ],
'show_in_menu' => true,
'show_in_rest' => true,
'show_tagcloud' => false,
'show_ui' => true,
];

// Register the taxonomies for all Listing CPTs.
$post_types = array_values( self::NEWSPACK_LISTINGS_POST_TYPES );
register_taxonomy( self::NEWSPACK_LISTINGS_CAT, $post_types, $category_args );
register_taxonomy( self::NEWSPACK_LISTINGS_TAG, $post_types, $tag_args );

// Better safe than sorry: https://developer.wordpress.org/reference/functions/register_taxonomy/#more-information.
foreach ( $post_types as $post_type ) {
register_taxonomy_for_object_type( self::NEWSPACK_LISTINGS_CAT, $post_type );
register_taxonomy_for_object_type( self::NEWSPACK_LISTINGS_TAG, $post_type );
}

// phpcs:ignore Squiz.PHP.CommentedOutCode.Found self::rewrite_taxonomy_urls();
}

/**
* Create rewrite rules for prefixed taxonomy URLs.
*/
public static function rewrite_taxonomy_urls() {
$prefix = Settings::get_settings( 'permalink_prefix' );

add_rewrite_rule( '^' . $prefix . '/category/([^/]+)/?$', 'index.php?' . self::NEWSPACK_LISTINGS_CAT . '=$matches[1]', 'top' );
add_rewrite_rule( '^' . $prefix . '/tag/([^/]+)/?$', 'index.php?' . self::NEWSPACK_LISTINGS_TAG . '=$matches[1]', 'top' );
}

/**
* Define and return meta fields for any Newspack Listings CPTs.
*
Expand Down Expand Up @@ -727,11 +658,10 @@ function( $classes ) {
}

/**
* Flush permalinks on plugin activation, ensuring that post types and taxonomies are registered first.
* Flush permalinks on plugin activation, ensuring that post types are registered first.
*/
public static function activation_hook() {
self::register_post_types();
self::register_taxonomies();
flush_rewrite_rules(); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.flush_rewrite_rules_flush_rewrite_rules
}
}
Expand Down
16 changes: 4 additions & 12 deletions src/components/sidebar-query-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,14 @@ class QueryControls extends Component {
return new Promise( resolve => resolve( [] ) );
}

const { taxonomies } = window.newspack_listings_data;

return apiFetch( {
path: addQueryArgs( '/newspack-listings/v1/terms', {
search,
per_page: 20,
_fields: 'id,name',
orderby: 'count',
order: 'desc',
taxonomy: taxonomies.category,
taxonomy: 'category',
} ),
} ).then( function( categories ) {
return categories.map( category => ( {
Expand All @@ -84,14 +82,12 @@ class QueryControls extends Component {
} );
};
fetchSavedCategories = categoryIDs => {
const { taxonomies } = window.newspack_listings_data;

return apiFetch( {
path: addQueryArgs( '/newspack-listings/v1/terms', {
per_page: 100,
_fields: 'id,name',
include: categoryIDs.join( ',' ),
taxonomy: taxonomies.category,
taxonomy: 'category',
} ),
} ).then( function( categories ) {
return categories.map( category => ( {
Expand All @@ -106,16 +102,14 @@ class QueryControls extends Component {
return new Promise( resolve => resolve( [] ) );
}

const { taxonomies } = window.newspack_listings_data;

return apiFetch( {
path: addQueryArgs( '/newspack-listings/v1/terms', {
search,
per_page: 20,
_fields: 'id,name',
orderby: 'count',
order: 'desc',
taxonomy: taxonomies.tag,
taxonomy: 'post_tag',
} ),
} ).then( function( tags ) {
return tags.map( tag => ( {
Expand All @@ -125,14 +119,12 @@ class QueryControls extends Component {
} );
};
fetchSavedTags = tagIDs => {
const { taxonomies } = window.newspack_listings_data;

return apiFetch( {
path: addQueryArgs( '/newspack-listings/v1/terms', {
per_page: 100,
_fields: 'id,name',
include: tagIDs.join( ',' ),
taxonomy: taxonomies.tag,
taxonomy: 'post_tag',
} ),
} ).then( function( tags ) {
return tags.map( tag => ( {
Expand Down
6 changes: 3 additions & 3 deletions src/templates/listing.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ function( $data ) {
<div class="newspack-listings__listing-meta">
<?php
if ( $attributes['showCategory'] ) :
$categories = get_the_terms( $post->ID, Core::NEWSPACK_LISTINGS_CAT );
$categories = get_the_terms( $post->ID, 'category' );

if ( is_array( $categories ) && 0 < count( $categories ) ) :
?>
<div class="newspack-listings__category cat-links">
<?php
$category_index = 0;
foreach ( $categories as $category ) {
$term_url = get_term_link( $category->slug, Core::NEWSPACK_LISTINGS_CAT );
$term_url = get_term_link( $category->slug, 'category' );

if ( empty( $term_url ) ) {
$term_url = '#';
Expand Down Expand Up @@ -82,7 +82,7 @@ function( $data ) {

<?php
if ( $attributes['showTags'] ) :
$tags = get_the_terms( $post->ID, Core::NEWSPACK_LISTINGS_TAG );
$tags = get_the_terms( $post->ID, 'post_tag' );

if ( is_array( $tags ) && 0 < count( $tags ) ) :
?>
Expand Down

0 comments on commit f223053

Please sign in to comment.