diff --git a/includes/class-newspack-listings-api.php b/includes/class-newspack-listings-api.php index f7ec901d..8f6e4dfe 100644 --- a/includes/class-newspack-listings-api.php +++ b/includes/class-newspack-listings-api.php @@ -146,21 +146,21 @@ 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', @@ -168,7 +168,7 @@ public static function build_listings_query( $query, $args = [] ) { } if ( ! empty( $query['tagExclusions'] ) ) { $args['tax_query'][] = [ - 'taxonomy' => Core::NEWSPACK_LISTINGS_TAG, + 'taxonomy' => 'post_tag', 'field' => 'term_id', 'terms' => $query['tagExclusions'], 'operator' => 'NOT IN', @@ -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. @@ -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 ); diff --git a/includes/class-newspack-listings-blocks.php b/includes/class-newspack-listings-blocks.php index b54de409..103a550a 100644 --- a/includes/class-newspack-listings-blocks.php +++ b/includes/class-newspack-listings-blocks.php @@ -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, diff --git a/includes/class-newspack-listings-core.php b/includes/class-newspack-listings-core.php index 165a65e0..986f5fec 100644 --- a/includes/class-newspack-listings-core.php +++ b/includes/class-newspack-listings-core.php @@ -16,7 +16,7 @@ /** * Main Core class. - * Sets up CPTs and taxonomies for listings. + * Sets up CPTs for listings. */ final class Newspack_Listings_Core { @@ -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. * @@ -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' ] ); @@ -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. @@ -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? * @@ -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' => [ @@ -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. * @@ -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 } } diff --git a/src/components/sidebar-query-controls.js b/src/components/sidebar-query-controls.js index 6a2c97b0..9f1239ed 100644 --- a/src/components/sidebar-query-controls.js +++ b/src/components/sidebar-query-controls.js @@ -65,8 +65,6 @@ class QueryControls extends Component { return new Promise( resolve => resolve( [] ) ); } - const { taxonomies } = window.newspack_listings_data; - return apiFetch( { path: addQueryArgs( '/newspack-listings/v1/terms', { search, @@ -74,7 +72,7 @@ class QueryControls extends Component { _fields: 'id,name', orderby: 'count', order: 'desc', - taxonomy: taxonomies.category, + taxonomy: 'category', } ), } ).then( function( categories ) { return categories.map( category => ( { @@ -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 => ( { @@ -106,8 +102,6 @@ class QueryControls extends Component { return new Promise( resolve => resolve( [] ) ); } - const { taxonomies } = window.newspack_listings_data; - return apiFetch( { path: addQueryArgs( '/newspack-listings/v1/terms', { search, @@ -115,7 +109,7 @@ class QueryControls extends Component { _fields: 'id,name', orderby: 'count', order: 'desc', - taxonomy: taxonomies.tag, + taxonomy: 'post_tag', } ), } ).then( function( tags ) { return tags.map( tag => ( { @@ -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 => ( { diff --git a/src/templates/listing.php b/src/templates/listing.php index b3993abc..d93061d2 100644 --- a/src/templates/listing.php +++ b/src/templates/listing.php @@ -41,7 +41,7 @@ function( $data ) {
ID, Core::NEWSPACK_LISTINGS_CAT ); + $categories = get_the_terms( $post->ID, 'category' ); if ( is_array( $categories ) && 0 < count( $categories ) ) : ?> @@ -49,7 +49,7 @@ function( $data ) { slug, Core::NEWSPACK_LISTINGS_CAT ); + $term_url = get_term_link( $category->slug, 'category' ); if ( empty( $term_url ) ) { $term_url = '#'; @@ -82,7 +82,7 @@ function( $data ) { ID, Core::NEWSPACK_LISTINGS_TAG ); + $tags = get_the_terms( $post->ID, 'post_tag' ); if ( is_array( $tags ) && 0 < count( $tags ) ) : ?>