Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add category filter for the categories in the blocks #1528

Merged
merged 3 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion includes/class-newspack-blocks-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ public static function newspack_blocks_get_primary_category( $object ) {
return '';
}

return $category->name;
$linked_category = '<a href="#">' . $category->name . '</a>';

return apply_filters( 'newspack_blocks_categories', $linked_category );
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/blocks/carousel/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -303,7 +303,7 @@ class Edit extends Component {
{ showCategory &&
( ! post.newspack_post_sponsors ||
post.newspack_sponsors_show_categories ) && (
<a href="#">{ decodeEntities( post.newspack_category_info ) }</a>
<RawHTML>{ decodeEntities( post.newspack_category_info ) }</RawHTML>
) }
</div>
) }
Expand Down
32 changes: 3 additions & 29 deletions src/blocks/carousel/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,36 +111,10 @@ function newspack_blocks_render_block_carousel( $attributes ) {
</span>
<?php
endif;
$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];
}
}

if ( $attributes['showCategory'] && $category && ( empty( $sponsors ) || Newspack_Blocks::newspack_display_sponsors_and_categories( $sponsors ) ) ) :
?>
<?php $category_link = get_category_link( $category->term_id ); ?>
<?php if ( ! empty( $category_link ) ) : ?>
<a href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>">
<?php endif; ?>
<?php echo esc_html( $category->name ); ?>
<?php if ( ! empty( $category_link ) ) : ?>
</a>
<?php
endif;
endif;
if ( $attributes['showCategory'] && ( empty( $sponsors ) || Newspack_Blocks::newspack_display_sponsors_and_categories( $sponsors ) ) ) :
echo wp_kses_post( newspack_blocks_format_categories( $post_id ) );
endif;
?>
</div>
<?php
Expand Down
2 changes: 1 addition & 1 deletion src/blocks/homepage-articles/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class Edit extends Component {
) }
{ showCategory &&
( ! post.newspack_post_sponsors || post.newspack_sponsors_show_categories ) && (
<a href="#">{ decodeEntities( post.newspack_category_info ) }</a>
<RawHTML>{ decodeEntities( post.newspack_category_info ) }</RawHTML>
) }
</div>
) }
Expand Down
32 changes: 9 additions & 23 deletions src/blocks/homepage-articles/templates/article.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -105,19 +90,20 @@ class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>"
<?php endif; ?>

<div class="entry-wrapper">
<?php if ( ! empty( $sponsors ) || ( $attributes['showCategory'] && $category ) ) : ?>
<?php $category_link = get_category_link( $category->term_id ); ?>
<?php if ( ! empty( $sponsors ) || ( $attributes['showCategory'] ) ) : ?>

<div class="cat-links <?php if ( ! empty( $sponsors ) ) : ?>sponsor-label<?php endif; // phpcs:ignore Squiz.ControlStructures.ControlSignature.NewlineAfterOpenBrace ?>">
<?php if ( ! empty( $sponsors ) ) : ?>
<span class="flag">
<?php echo esc_html( Newspack_Blocks::get_sponsor_label( $sponsors ) ); ?>
</span>
<?php endif; ?>
<?php if ( $attributes['showCategory'] && ! empty( $category_link ) && ( empty( $sponsors ) || Newspack_Blocks::newspack_display_sponsors_and_categories( $sponsors ) ) ) : ?>
<a href="<?php echo esc_url( $category_link ); ?>">
<?php echo esc_html( $category->name ); ?>
</a>
<?php endif; ?>
<?php
endif;

if ( $attributes['showCategory'] && ( empty( $sponsors ) || Newspack_Blocks::newspack_display_sponsors_and_categories( $sponsors ) ) ) :
echo wp_kses_post( newspack_blocks_format_categories( $post_id ) );
endif;
?>
</div>
<?php
endif;
Expand Down
34 changes: 34 additions & 0 deletions src/blocks/homepage-articles/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,40 @@ function ( $accumulator, $author ) use ( $author_info, &$index ) {
return implode( '', $elements );
}

/**
* Renders category markup plus filter.
*
* @param string $post_id Post ID.
*/
function newspack_blocks_format_categories( $post_id ) {
$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];
}
}

$category_link = get_category_link( $category->term_id );
$category_formatted = esc_html( $category->name );

if ( ! empty( $category_link ) ) {
$category_formatted = '<a href="' . esc_attr( $category_link ) . '">' . esc_html( $category->name ) . '</a>';
}

if ( $category ) {
return apply_filters( 'newspack_blocks_categories', $category_formatted );
}
}

/**
* Inject amp-state containing all post IDs visible on page load.
*/
Expand Down