Skip to content

Commit

Permalink
feat: add sponsor labels to homepage posts and post carousel blocks (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
laurelfulford authored Aug 17, 2020
1 parent e0a5a83 commit cbcd65f
Show file tree
Hide file tree
Showing 7 changed files with 399 additions and 79 deletions.
44 changes: 44 additions & 0 deletions class-newspack-blocks-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ public static function register_rest_fields() {
],
]
);

/* Sponsors */
register_rest_field(
'post',
'newspack_post_sponsors',
[
'get_callback' => [ 'Newspack_Blocks_API', 'newspack_blocks_sponsor_info' ],
'schema' => [
'context' => [
'edit',
],
'type' => 'array',
],
]
);
}

/**
Expand Down Expand Up @@ -261,6 +276,35 @@ public static function newspack_blocks_get_cat_tag_classes( $object ) {
return Newspack_Blocks::get_term_classes( $object['id'] );
}

/**
* Get all sponsor information for the rest field.
*
* @param array $object The object info.
* @return array sponsor information.
*/
public static function newspack_blocks_sponsor_info( $object ) {
if ( Newspack_Blocks::get_post_sponsors( $object['id'] ) ) {
$sponsors = Newspack_Blocks::get_post_sponsors( $object['id'] );
foreach ( $sponsors as $sponsor ) {
$sponsor_logo = Newspack_Blocks::get_sponsor_logo_sized( $sponsor['sponsor_id'] );
$sponsor_info[] = array(
'flag' => $sponsor['sponsor_flag'],
'sponsor_name' => $sponsor['sponsor_name'],
'sponsor_url' => $sponsor['sponsor_url'],
'byline_prefix' => $sponsor['sponsor_byline'],
'id' => $sponsor['sponsor_id'],
'scope' => $sponsor['sponsor_scope'],
'src' => $sponsor_logo['src'],
'img_width' => $sponsor_logo['img_width'],
'img_height' => $sponsor_logo['img_height'],
);
}
return $sponsor_info;
} else {
return false;
}
}

/**
* Register the video-playlist endpoint.
*/
Expand Down
139 changes: 139 additions & 0 deletions class-newspack-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,145 @@ public static function get_patterns_for_post_type( $post_type = null ) {
return $clean;
}

/**
* Function to check if plugin is enabled, and if there are sponsors.
*
* @param string $post_id Post ID.
* @return array Array of sponsors.
*/
public static function get_post_sponsors( $post_id ) {
if ( function_exists( '\Newspack_Sponsors\get_sponsors_for_post' ) ) {
// Get all assigned sponsors.
$sponsors_all = \Newspack_Sponsors\get_sponsors_for_post( $post_id );

// Loop through sponsors and remove duplicates.
$sponsors = array();
$duplicates = array();
foreach ( $sponsors_all as $sponsor ) {
// For the blocks, only add visual elements to 'native' sponsored content.
if ( 'native' !== $sponsor['sponsor_scope'] ) {
continue;
}
if ( ! in_array( $sponsor['sponsor_id'], $duplicates, true ) ) {
$duplicates[] = $sponsor['sponsor_id'];
$sponsors[] = $sponsor;
}
}
}
if ( $sponsors ) {
return $sponsors;
}
return false;
}

/**
* Function to return sponsor 'flag' from first sponsor.
*
* @param string $post_id Post ID.
* @return string Sponsor flag label.
*/
public static function get_sponsor_label( $post_id ) {
if ( self::get_post_sponsors( $post_id ) ) {
$sponsors = self::get_post_sponsors( $post_id );
$sponsor_flag = $sponsors[0]['sponsor_flag'];

return $sponsor_flag;
}
}

/**
* Outputs the sponsor byline markup for the theme.
*
* @param string $post_id Post ID.
* @return array Array of Sponsor byline information.
*/
public static function get_sponsor_byline( $post_id ) {
if ( self::get_post_sponsors( $post_id ) ) {
$sponsors = self::get_post_sponsors( $post_id );
$sponsor_count = count( $sponsors );
$i = 1;
$sponsor_list = [];

foreach ( $sponsors as $sponsor ) {
$i++;
if ( $sponsor_count === $i ) :
/* translators: separates last two sponsor names; needs a space on either side. */
$sep = esc_html__( ' and ', 'newspack-blocks' );
elseif ( $sponsor_count > $i ) :
/* translators: separates all but the last two sponsor names; needs a space at the end. */
$sep = esc_html__( ', ', 'newspack-blocks' );
else :
$sep = '';
endif;

$sponsor_list[] = array(
'byline' => $sponsor['sponsor_byline'],
'url' => $sponsor['sponsor_url'],
'name' => $sponsor['sponsor_name'],
'sep' => $sep,
);
}
return $sponsor_list;
}
}

/**
* Outputs set of sponsor logos with links.
*
* @param string $post_id Post ID.
*/
public static function get_sponsor_logos( $post_id ) {
if ( self::get_post_sponsors( $post_id ) ) {
$sponsors = self::get_post_sponsors( $post_id );
$sponsor_logos = [];

foreach ( $sponsors as $sponsor ) {
if ( '' !== $sponsor['sponsor_logo'] ) :
$logo_info = self::get_sponsor_logo_sized( $sponsor['sponsor_id'] );

$sponsor_logos[] = array(
'url' => $sponsor['sponsor_url'],
'src' => esc_url( $logo_info['src'] ),
'width' => esc_attr( $logo_info['img_width'] ),
'height' => esc_attr( $logo_info['img_height'] ),
);
endif;
}

return $sponsor_logos;
}
}

/**
* Returns scaled down logo sizes based on the provided width and height; this is necessary for AMP.
*
* @param string $sponsor_id Sponsor ID.
* @param string $maxwidth Maximum logo width.
* @param string $maxheight Maximum logo height.
* @return array Array with image's src, width and height.
*/
public static function get_sponsor_logo_sized( $sponsor_id, $maxwidth = 80, $maxheight = 40 ) {
// Get image information.
$image_info = wp_get_attachment_image_src( get_post_thumbnail_id( $sponsor_id ), 'medium' );

// Break out URL, original width and original height.
$logo_info['src'] = $image_info[0];
$image_width = $image_info[1];
$image_height = $image_info[2];

// Set the max-height, and width based off that to maintain aspect ratio.
$logo_info['img_height'] = $maxheight;
$logo_info['img_width'] = ( $image_width / $image_height ) * $logo_info['img_height'];

// If the new width is too wide, set to the max-width and update height based off that to maintain aspect ratio.
if ( $maxwidth < $logo_info['img_width'] ) {
$logo_info['img_width'] = $maxwidth;
$logo_info['img_height'] = ( $image_height / $image_width ) * $logo_info['img_width'];
}

return $logo_info;
}

/**
* Whether to use experimental features.
*
Expand Down
33 changes: 27 additions & 6 deletions src/blocks/carousel/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ import { decodeEntities } from '@wordpress/html-entities';
*/
import QueryControls from '../../components/query-controls';
import createSwiper from './create-swiper';
import { formatAvatars, formatByline } from '../../shared/js/utils';
import {
formatAvatars,
formatByline,
formatSponsorLogos,
formatSponsorByline,
} from '../../shared/js/utils';

class Edit extends Component {
constructor( props ) {
Expand Down Expand Up @@ -153,19 +158,35 @@ class Edit extends Component {
) }
</figure>
<div className="entry-wrapper">
{ showCategory && post.newspack_category_info.length && (
<div className="cat-links">
<a href="#">{ decodeEntities( post.newspack_category_info ) }</a>
</div>
{ post.newspack_post_sponsors && (
<span className="cat-links sponsor-label">
<span className="flag">
{ post.newspack_post_sponsors[ 0 ].flag }
</span>
</span>
) }
{ showCategory &&
post.newspack_category_info.length &&
! post.newspack_post_sponsors && (
<div className="cat-links">
<a href="#">{ decodeEntities( post.newspack_category_info ) }</a>
</div>
) }
<h3 className="entry-title">
<a href="#">{ decodeEntities( post.title.rendered.trim() ) }</a>
</h3>
<div className="entry-meta">
{ post.newspack_post_sponsors &&
formatSponsorLogos( post.newspack_post_sponsors ) }
{ post.newspack_post_sponsors &&
formatSponsorByline( post.newspack_post_sponsors ) }
{ showAuthor &&
showAvatar &&
! post.newspack_post_sponsors &&
formatAvatars( post.newspack_author_info ) }
{ showAuthor && formatByline( post.newspack_author_info ) }
{ showAuthor &&
! post.newspack_post_sponsors &&
formatByline( post.newspack_author_info ) }
{ showDate && (
<time className="entry-date published" key="pub-date">
{ dateI18n( dateFormat, post.date_gmt ) }
Expand Down
117 changes: 75 additions & 42 deletions src/blocks/carousel/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,33 +87,41 @@ function newspack_blocks_render_block_carousel( $attributes ) {
</figure>
<div class="entry-wrapper">

<?php if ( Newspack_Blocks::get_post_sponsors( get_the_id() ) ) : ?>
<span class="cat-links sponsor-label">
<span class="flag">
<?php echo esc_html( Newspack_Blocks::get_sponsor_label( get_the_id() ) ); ?>
</span>
</span>
<?php
$category = false;
else :
$category = false;

// Use Yoast primary category if set.
if ( class_exists( 'WPSEO_Primary_Term' ) ) {
$primary_term = new WPSEO_Primary_Term( 'category', get_the_ID() );
$category_id = $primary_term->get_primary_term();
if ( $category_id ) {
$category = get_term( $category_id );
// Use Yoast primary category if set.
if ( class_exists( 'WPSEO_Primary_Term' ) ) {
$primary_term = new WPSEO_Primary_Term( 'category', get_the_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 ( ! $category ) {
$categories_list = get_the_category();
if ( ! empty( $categories_list ) ) {
$category = $categories_list[0];
}
}
}

if ( $attributes['showCategory'] && $category ) :
?>
<div class="cat-links">
<a href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>">
<?php echo esc_html( $category->name ); ?>
</a>
</div>
<?php
if ( $attributes['showCategory'] && $category ) :
?>
<div class="cat-links">
<a href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>">
<?php echo esc_html( $category->name ); ?>
</a>
</div>
<?php
endif;
endif;
?>

Expand All @@ -122,31 +130,56 @@ function newspack_blocks_render_block_carousel( $attributes ) {
?>

<div class="entry-meta">
<?php if ( $attributes['showAuthor'] ) : ?>
<?php
if ( $attributes['showAvatar'] ) {
echo get_avatar( get_the_author_meta( 'ID' ) );
}
?>
<span class="byline">
<?php if ( Newspack_Blocks::get_post_sponsors( get_the_id() ) ) : ?>
<span class="sponsor-logos">
<?php
$logos = Newspack_Blocks::get_sponsor_logos( get_the_id() );
foreach ( $logos as $logo ) {
if ( '' !== $logo['url'] ) {
echo '<a href="' . esc_url( $logo['url'] ) . '" target="_blank">';
}
echo '<img src="' . esc_url( $logo['src'] ) . '" width="' . esc_attr( $logo['width'] ) . '" height="' . esc_attr( $logo['height'] ) . '">';
if ( '' !== $logo['url'] ) {
echo '</a>';
}
}
?>
</span>
<span class="byline sponsor-byline">
<?php
printf(
/* translators: %s: post author. */
esc_html_x( 'by %s', 'post author', 'newspack-blocks' ),
'<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>'
);
$bylines = Newspack_Blocks::get_sponsor_byline( get_the_id() );
echo esc_html( $bylines[0]['byline'] ) . ' ';
foreach ( $bylines as $byline ) {
echo '<span class="author"><a target="_blank" href="' . esc_url( $byline['url'] ) . '">' . esc_html( $byline['name'] ) . '</a></span>' . esc_html( $byline['sep'] );
}
?>
</span><!-- .author-name -->
</span>
<?php
else :
if ( $attributes['showAuthor'] ) :
if ( $attributes['showAvatar'] ) {
echo get_avatar( get_the_author_meta( 'ID' ) );
}
?>
<span class="byline">
<?php
printf(
/* translators: %s: post author. */
esc_html_x( 'by %s', 'post author', 'newspack-blocks' ),
'<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author() ) . '</a></span>'
);
?>
</span><!-- .author-name -->
<?php
endif;

if ( $attributes['showDate'] ) :
printf(
'<time class="entry-date published" datetime="%1$s">%2$s</time>',
esc_attr( get_the_date( DATE_W3C ) ),
esc_html( get_the_date() )
);
endif;
endif;
if ( $attributes['showDate'] ) :
printf(
'<time class="entry-date published" datetime="%1$s">%2$s</time>',
esc_attr( get_the_date( DATE_W3C ) ),
esc_html( get_the_date() )
);
endif;
?>
</div><!-- .entry-meta -->
</div><!-- .entry-wrapper -->
Expand Down
Loading

0 comments on commit cbcd65f

Please sign in to comment.