From 9d1fc24fa8547aa8cae267dea3b30567824afc30 Mon Sep 17 00:00:00 2001 From: Laurel Fulford Date: Thu, 14 Sep 2023 09:16:59 -0700 Subject: [PATCH 1/3] feat: add filter to block categories --- includes/class-newspack-blocks-api.php | 4 ++- src/blocks/carousel/edit.js | 4 +-- src/blocks/carousel/view.php | 32 ++----------------- src/blocks/homepage-articles/edit.js | 2 +- .../homepage-articles/templates/article.php | 32 ++++++------------- src/blocks/homepage-articles/view.php | 29 +++++++++++++++++ 6 files changed, 47 insertions(+), 56 deletions(-) diff --git a/includes/class-newspack-blocks-api.php b/includes/class-newspack-blocks-api.php index df7f77f36..7ec10dfe5 100644 --- a/includes/class-newspack-blocks-api.php +++ b/includes/class-newspack-blocks-api.php @@ -159,7 +159,9 @@ public static function newspack_blocks_get_primary_category( $object ) { return ''; } - return $category->name; + $linked_category = '' . $category->name . ''; + + return apply_filters( 'newspack_block_categories', $linked_category ); } /** diff --git a/src/blocks/carousel/edit.js b/src/blocks/carousel/edit.js index e69d0b91c..60dd904ec 100644 --- a/src/blocks/carousel/edit.js +++ b/src/blocks/carousel/edit.js @@ -13,7 +13,7 @@ import { __ } from '@wordpress/i18n'; import { InspectorControls } from '@wordpress/block-editor'; // eslint-disable-next-line @wordpress/no-unsafe-wp-apis import { dateI18n, __experimentalGetSettings } from '@wordpress/date'; -import { Component, createRef, Fragment } from '@wordpress/element'; +import { Component, createRef, Fragment, RawHTML } from '@wordpress/element'; import { BaseControl, Button, @@ -303,7 +303,7 @@ class Edit extends Component { { showCategory && ( ! post.newspack_post_sponsors || post.newspack_sponsors_show_categories ) && ( - { decodeEntities( post.newspack_category_info ) } + { decodeEntities( post.newspack_category_info ) } ) } ) } diff --git a/src/blocks/carousel/view.php b/src/blocks/carousel/view.php index 1c48ac005..c49110d83 100644 --- a/src/blocks/carousel/view.php +++ b/src/blocks/carousel/view.php @@ -111,36 +111,10 @@ function newspack_blocks_render_block_carousel( $attributes ) { get_primary_term(); - if ( $category_id ) { - $category = get_term( $category_id ); - } - } - - if ( ! $category ) { - $categories_list = get_the_category(); - if ( ! empty( $categories_list ) ) { - $category = $categories_list[0]; - } - } - - if ( $attributes['showCategory'] && $category && ( empty( $sponsors ) || Newspack_Blocks::newspack_display_sponsors_and_categories( $sponsors ) ) ) : - ?> - term_id ); ?> - - - - name ); ?> - - - { decodeEntities( post.newspack_category_info ) } + { decodeEntities( post.newspack_category_info ) } ) } ) } diff --git a/src/blocks/homepage-articles/templates/article.php b/src/blocks/homepage-articles/templates/article.php index 547a4d026..b1375cc7a 100644 --- a/src/blocks/homepage-articles/templates/article.php +++ b/src/blocks/homepage-articles/templates/article.php @@ -56,21 +56,6 @@ function( $data ) { if ( $attributes['fetchPriority'] && in_array( $attributes['fetchPriority'], [ 'high', 'low', 'auto' ], true ) ) { $thumbnail_args['fetchpriority'] = $attributes['fetchPriority']; } - $category = false; - // Use Yoast primary category if set. - if ( class_exists( 'WPSEO_Primary_Term' ) ) { - $primary_term = new WPSEO_Primary_Term( 'category', $post_id ); - $category_id = $primary_term->get_primary_term(); - if ( $category_id ) { - $category = get_term( $category_id ); - } - } - if ( ! $category ) { - $categories_list = get_the_category(); - if ( ! empty( $categories_list ) ) { - $category = $categories_list[0]; - } - } // Support Newspack Listings hide author/publish date options. $hide_author = apply_filters( 'newspack_listings_hide_author', false ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound @@ -105,19 +90,20 @@ class=""
- - term_id ); ?> + + get_primary_term(); + if ( $category_id ) { + $category = get_term( $category_id ); + } + } + if ( ! $category ) { + $categories_list = get_the_category(); + if ( ! empty( $categories_list ) ) { + $category = $categories_list[0]; + } + } + + $category_link = get_category_link( $category->term_id ); + $category_formatted = esc_html( $category->name ); + + if ( ! empty( $category_link ) ) { + $category_formatted = "" . esc_html( $category->name ) . ""; + } + + if ( $category ) { + echo apply_filters( 'newspack_block_categories', $category_formatted ); + } +} + /** * Inject amp-state containing all post IDs visible on page load. */ From 17f3c348ae91a8569048f29d2d848c796c5411a8 Mon Sep 17 00:00:00 2001 From: Laurel Fulford Date: Thu, 14 Sep 2023 09:27:23 -0700 Subject: [PATCH 2/3] feat: add filter to block categories --- includes/class-newspack-blocks-api.php | 2 +- src/blocks/homepage-articles/view.php | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/includes/class-newspack-blocks-api.php b/includes/class-newspack-blocks-api.php index 7ec10dfe5..91d07277c 100644 --- a/includes/class-newspack-blocks-api.php +++ b/includes/class-newspack-blocks-api.php @@ -161,7 +161,7 @@ public static function newspack_blocks_get_primary_category( $object ) { $linked_category = '' . $category->name . ''; - return apply_filters( 'newspack_block_categories', $linked_category ); + return wp_kses_post( apply_filters( 'newspack_blocks_categories', $linked_category ) ); } /** diff --git a/src/blocks/homepage-articles/view.php b/src/blocks/homepage-articles/view.php index e946f48dd..e25e1bd24 100644 --- a/src/blocks/homepage-articles/view.php +++ b/src/blocks/homepage-articles/view.php @@ -337,6 +337,13 @@ function ( $accumulator, $author ) use ( $author_info, &$index ) { return implode( '', $elements ); } +/** + * Renders category markup plus filter. + * + * @param string $post_id Post ID. + * + * @return string Returns filtered category markup. + */ function newspack_blocks_categories( $post_id ) { $category = false; // Use Yoast primary category if set. @@ -354,15 +361,15 @@ function newspack_blocks_categories( $post_id ) { } } - $category_link = get_category_link( $category->term_id ); + $category_link = get_category_link( $category->term_id ); $category_formatted = esc_html( $category->name ); if ( ! empty( $category_link ) ) { - $category_formatted = "" . esc_html( $category->name ) . ""; + $category_formatted = '' . esc_html( $category->name ) . ''; } if ( $category ) { - echo apply_filters( 'newspack_block_categories', $category_formatted ); + echo apply_filters( 'newspack_blocks_categories', $category_formatted ); } } From 4df7aa1fa0122e287b8c36c793d7900fe15c0fb5 Mon Sep 17 00:00:00 2001 From: Laurel Fulford Date: Thu, 14 Sep 2023 09:34:59 -0700 Subject: [PATCH 3/3] fix: correct linting errors --- includes/class-newspack-blocks-api.php | 2 +- src/blocks/carousel/view.php | 2 +- src/blocks/homepage-articles/templates/article.php | 2 +- src/blocks/homepage-articles/view.php | 6 ++---- 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/includes/class-newspack-blocks-api.php b/includes/class-newspack-blocks-api.php index 91d07277c..c52301020 100644 --- a/includes/class-newspack-blocks-api.php +++ b/includes/class-newspack-blocks-api.php @@ -161,7 +161,7 @@ public static function newspack_blocks_get_primary_category( $object ) { $linked_category = '' . $category->name . ''; - return wp_kses_post( apply_filters( 'newspack_blocks_categories', $linked_category ) ); + return apply_filters( 'newspack_blocks_categories', $linked_category ); } /** diff --git a/src/blocks/carousel/view.php b/src/blocks/carousel/view.php index c49110d83..ab90772f2 100644 --- a/src/blocks/carousel/view.php +++ b/src/blocks/carousel/view.php @@ -113,7 +113,7 @@ function newspack_blocks_render_block_carousel( $attributes ) { endif; if ( $attributes['showCategory'] && ( empty( $sponsors ) || Newspack_Blocks::newspack_display_sponsors_and_categories( $sponsors ) ) ) : - newspack_blocks_categories( $post_id ); + echo wp_kses_post( newspack_blocks_format_categories( $post_id ) ); endif; ?>
diff --git a/src/blocks/homepage-articles/templates/article.php b/src/blocks/homepage-articles/templates/article.php index b1375cc7a..f11439096 100644 --- a/src/blocks/homepage-articles/templates/article.php +++ b/src/blocks/homepage-articles/templates/article.php @@ -101,7 +101,7 @@ class="" endif; if ( $attributes['showCategory'] && ( empty( $sponsors ) || Newspack_Blocks::newspack_display_sponsors_and_categories( $sponsors ) ) ) : - newspack_blocks_categories( $post_id ); + echo wp_kses_post( newspack_blocks_format_categories( $post_id ) ); endif; ?> diff --git a/src/blocks/homepage-articles/view.php b/src/blocks/homepage-articles/view.php index e25e1bd24..fab045aef 100644 --- a/src/blocks/homepage-articles/view.php +++ b/src/blocks/homepage-articles/view.php @@ -341,10 +341,8 @@ function ( $accumulator, $author ) use ( $author_info, &$index ) { * Renders category markup plus filter. * * @param string $post_id Post ID. - * - * @return string Returns filtered category markup. */ -function newspack_blocks_categories( $post_id ) { +function newspack_blocks_format_categories( $post_id ) { $category = false; // Use Yoast primary category if set. if ( class_exists( 'WPSEO_Primary_Term' ) ) { @@ -369,7 +367,7 @@ function newspack_blocks_categories( $post_id ) { } if ( $category ) { - echo apply_filters( 'newspack_blocks_categories', $category_formatted ); + return apply_filters( 'newspack_blocks_categories', $category_formatted ); } }