Skip to content

Commit 04102ee

Browse files
authored
Merge pull request #572 from Automattic/master
Alpha release Aug 18
2 parents d0a091e + cbcd65f commit 04102ee

File tree

7 files changed

+399
-79
lines changed

7 files changed

+399
-79
lines changed

class-newspack-blocks-api.php

+44
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,21 @@ public static function register_rest_fields() {
8686
],
8787
]
8888
);
89+
90+
/* Sponsors */
91+
register_rest_field(
92+
'post',
93+
'newspack_post_sponsors',
94+
[
95+
'get_callback' => [ 'Newspack_Blocks_API', 'newspack_blocks_sponsor_info' ],
96+
'schema' => [
97+
'context' => [
98+
'edit',
99+
],
100+
'type' => 'array',
101+
],
102+
]
103+
);
89104
}
90105

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

279+
/**
280+
* Get all sponsor information for the rest field.
281+
*
282+
* @param array $object The object info.
283+
* @return array sponsor information.
284+
*/
285+
public static function newspack_blocks_sponsor_info( $object ) {
286+
if ( Newspack_Blocks::get_post_sponsors( $object['id'] ) ) {
287+
$sponsors = Newspack_Blocks::get_post_sponsors( $object['id'] );
288+
foreach ( $sponsors as $sponsor ) {
289+
$sponsor_logo = Newspack_Blocks::get_sponsor_logo_sized( $sponsor['sponsor_id'] );
290+
$sponsor_info[] = array(
291+
'flag' => $sponsor['sponsor_flag'],
292+
'sponsor_name' => $sponsor['sponsor_name'],
293+
'sponsor_url' => $sponsor['sponsor_url'],
294+
'byline_prefix' => $sponsor['sponsor_byline'],
295+
'id' => $sponsor['sponsor_id'],
296+
'scope' => $sponsor['sponsor_scope'],
297+
'src' => $sponsor_logo['src'],
298+
'img_width' => $sponsor_logo['img_width'],
299+
'img_height' => $sponsor_logo['img_height'],
300+
);
301+
}
302+
return $sponsor_info;
303+
} else {
304+
return false;
305+
}
306+
}
307+
264308
/**
265309
* Register the video-playlist endpoint.
266310
*/

class-newspack-blocks.php

+139
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,145 @@ public static function get_patterns_for_post_type( $post_type = null ) {
502502
return $clean;
503503
}
504504

505+
/**
506+
* Function to check if plugin is enabled, and if there are sponsors.
507+
*
508+
* @param string $post_id Post ID.
509+
* @return array Array of sponsors.
510+
*/
511+
public static function get_post_sponsors( $post_id ) {
512+
if ( function_exists( '\Newspack_Sponsors\get_sponsors_for_post' ) ) {
513+
// Get all assigned sponsors.
514+
$sponsors_all = \Newspack_Sponsors\get_sponsors_for_post( $post_id );
515+
516+
// Loop through sponsors and remove duplicates.
517+
$sponsors = array();
518+
$duplicates = array();
519+
foreach ( $sponsors_all as $sponsor ) {
520+
// For the blocks, only add visual elements to 'native' sponsored content.
521+
if ( 'native' !== $sponsor['sponsor_scope'] ) {
522+
continue;
523+
}
524+
if ( ! in_array( $sponsor['sponsor_id'], $duplicates, true ) ) {
525+
$duplicates[] = $sponsor['sponsor_id'];
526+
$sponsors[] = $sponsor;
527+
}
528+
}
529+
}
530+
if ( $sponsors ) {
531+
return $sponsors;
532+
}
533+
return false;
534+
}
535+
536+
/**
537+
* Function to return sponsor 'flag' from first sponsor.
538+
*
539+
* @param string $post_id Post ID.
540+
* @return string Sponsor flag label.
541+
*/
542+
public static function get_sponsor_label( $post_id ) {
543+
if ( self::get_post_sponsors( $post_id ) ) {
544+
$sponsors = self::get_post_sponsors( $post_id );
545+
$sponsor_flag = $sponsors[0]['sponsor_flag'];
546+
547+
return $sponsor_flag;
548+
}
549+
}
550+
551+
/**
552+
* Outputs the sponsor byline markup for the theme.
553+
*
554+
* @param string $post_id Post ID.
555+
* @return array Array of Sponsor byline information.
556+
*/
557+
public static function get_sponsor_byline( $post_id ) {
558+
if ( self::get_post_sponsors( $post_id ) ) {
559+
$sponsors = self::get_post_sponsors( $post_id );
560+
$sponsor_count = count( $sponsors );
561+
$i = 1;
562+
$sponsor_list = [];
563+
564+
foreach ( $sponsors as $sponsor ) {
565+
$i++;
566+
if ( $sponsor_count === $i ) :
567+
/* translators: separates last two sponsor names; needs a space on either side. */
568+
$sep = esc_html__( ' and ', 'newspack-blocks' );
569+
elseif ( $sponsor_count > $i ) :
570+
/* translators: separates all but the last two sponsor names; needs a space at the end. */
571+
$sep = esc_html__( ', ', 'newspack-blocks' );
572+
else :
573+
$sep = '';
574+
endif;
575+
576+
$sponsor_list[] = array(
577+
'byline' => $sponsor['sponsor_byline'],
578+
'url' => $sponsor['sponsor_url'],
579+
'name' => $sponsor['sponsor_name'],
580+
'sep' => $sep,
581+
);
582+
}
583+
return $sponsor_list;
584+
}
585+
}
586+
587+
/**
588+
* Outputs set of sponsor logos with links.
589+
*
590+
* @param string $post_id Post ID.
591+
*/
592+
public static function get_sponsor_logos( $post_id ) {
593+
if ( self::get_post_sponsors( $post_id ) ) {
594+
$sponsors = self::get_post_sponsors( $post_id );
595+
$sponsor_logos = [];
596+
597+
foreach ( $sponsors as $sponsor ) {
598+
if ( '' !== $sponsor['sponsor_logo'] ) :
599+
$logo_info = self::get_sponsor_logo_sized( $sponsor['sponsor_id'] );
600+
601+
$sponsor_logos[] = array(
602+
'url' => $sponsor['sponsor_url'],
603+
'src' => esc_url( $logo_info['src'] ),
604+
'width' => esc_attr( $logo_info['img_width'] ),
605+
'height' => esc_attr( $logo_info['img_height'] ),
606+
);
607+
endif;
608+
}
609+
610+
return $sponsor_logos;
611+
}
612+
}
613+
614+
/**
615+
* Returns scaled down logo sizes based on the provided width and height; this is necessary for AMP.
616+
*
617+
* @param string $sponsor_id Sponsor ID.
618+
* @param string $maxwidth Maximum logo width.
619+
* @param string $maxheight Maximum logo height.
620+
* @return array Array with image's src, width and height.
621+
*/
622+
public static function get_sponsor_logo_sized( $sponsor_id, $maxwidth = 80, $maxheight = 40 ) {
623+
// Get image information.
624+
$image_info = wp_get_attachment_image_src( get_post_thumbnail_id( $sponsor_id ), 'medium' );
625+
626+
// Break out URL, original width and original height.
627+
$logo_info['src'] = $image_info[0];
628+
$image_width = $image_info[1];
629+
$image_height = $image_info[2];
630+
631+
// Set the max-height, and width based off that to maintain aspect ratio.
632+
$logo_info['img_height'] = $maxheight;
633+
$logo_info['img_width'] = ( $image_width / $image_height ) * $logo_info['img_height'];
634+
635+
// If the new width is too wide, set to the max-width and update height based off that to maintain aspect ratio.
636+
if ( $maxwidth < $logo_info['img_width'] ) {
637+
$logo_info['img_width'] = $maxwidth;
638+
$logo_info['img_height'] = ( $image_height / $image_width ) * $logo_info['img_width'];
639+
}
640+
641+
return $logo_info;
642+
}
643+
505644
/**
506645
* Whether to use experimental features.
507646
*

src/blocks/carousel/edit.js

+27-6
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ import { decodeEntities } from '@wordpress/html-entities';
3030
*/
3131
import QueryControls from '../../components/query-controls';
3232
import createSwiper from './create-swiper';
33-
import { formatAvatars, formatByline } from '../../shared/js/utils';
33+
import {
34+
formatAvatars,
35+
formatByline,
36+
formatSponsorLogos,
37+
formatSponsorByline,
38+
} from '../../shared/js/utils';
3439

3540
class Edit extends Component {
3641
constructor( props ) {
@@ -153,19 +158,35 @@ class Edit extends Component {
153158
) }
154159
</figure>
155160
<div className="entry-wrapper">
156-
{ showCategory && post.newspack_category_info.length && (
157-
<div className="cat-links">
158-
<a href="#">{ decodeEntities( post.newspack_category_info ) }</a>
159-
</div>
161+
{ post.newspack_post_sponsors && (
162+
<span className="cat-links sponsor-label">
163+
<span className="flag">
164+
{ post.newspack_post_sponsors[ 0 ].flag }
165+
</span>
166+
</span>
160167
) }
168+
{ showCategory &&
169+
post.newspack_category_info.length &&
170+
! post.newspack_post_sponsors && (
171+
<div className="cat-links">
172+
<a href="#">{ decodeEntities( post.newspack_category_info ) }</a>
173+
</div>
174+
) }
161175
<h3 className="entry-title">
162176
<a href="#">{ decodeEntities( post.title.rendered.trim() ) }</a>
163177
</h3>
164178
<div className="entry-meta">
179+
{ post.newspack_post_sponsors &&
180+
formatSponsorLogos( post.newspack_post_sponsors ) }
181+
{ post.newspack_post_sponsors &&
182+
formatSponsorByline( post.newspack_post_sponsors ) }
165183
{ showAuthor &&
166184
showAvatar &&
185+
! post.newspack_post_sponsors &&
167186
formatAvatars( post.newspack_author_info ) }
168-
{ showAuthor && formatByline( post.newspack_author_info ) }
187+
{ showAuthor &&
188+
! post.newspack_post_sponsors &&
189+
formatByline( post.newspack_author_info ) }
169190
{ showDate && (
170191
<time className="entry-date published" key="pub-date">
171192
{ dateI18n( dateFormat, post.date_gmt ) }

src/blocks/carousel/view.php

+75-42
Original file line numberDiff line numberDiff line change
@@ -87,33 +87,41 @@ function newspack_blocks_render_block_carousel( $attributes ) {
8787
</figure>
8888
<div class="entry-wrapper">
8989

90+
<?php if ( Newspack_Blocks::get_post_sponsors( get_the_id() ) ) : ?>
91+
<span class="cat-links sponsor-label">
92+
<span class="flag">
93+
<?php echo esc_html( Newspack_Blocks::get_sponsor_label( get_the_id() ) ); ?>
94+
</span>
95+
</span>
9096
<?php
91-
$category = false;
97+
else :
98+
$category = false;
9299

93-
// Use Yoast primary category if set.
94-
if ( class_exists( 'WPSEO_Primary_Term' ) ) {
95-
$primary_term = new WPSEO_Primary_Term( 'category', get_the_ID() );
96-
$category_id = $primary_term->get_primary_term();
97-
if ( $category_id ) {
98-
$category = get_term( $category_id );
100+
// Use Yoast primary category if set.
101+
if ( class_exists( 'WPSEO_Primary_Term' ) ) {
102+
$primary_term = new WPSEO_Primary_Term( 'category', get_the_ID() );
103+
$category_id = $primary_term->get_primary_term();
104+
if ( $category_id ) {
105+
$category = get_term( $category_id );
106+
}
99107
}
100-
}
101108

102-
if ( ! $category ) {
103-
$categories_list = get_the_category();
104-
if ( ! empty( $categories_list ) ) {
105-
$category = $categories_list[0];
109+
if ( ! $category ) {
110+
$categories_list = get_the_category();
111+
if ( ! empty( $categories_list ) ) {
112+
$category = $categories_list[0];
113+
}
106114
}
107-
}
108115

109-
if ( $attributes['showCategory'] && $category ) :
110-
?>
111-
<div class="cat-links">
112-
<a href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>">
113-
<?php echo esc_html( $category->name ); ?>
114-
</a>
115-
</div>
116-
<?php
116+
if ( $attributes['showCategory'] && $category ) :
117+
?>
118+
<div class="cat-links">
119+
<a href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>">
120+
<?php echo esc_html( $category->name ); ?>
121+
</a>
122+
</div>
123+
<?php
124+
endif;
117125
endif;
118126
?>
119127

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

124132
<div class="entry-meta">
125-
<?php if ( $attributes['showAuthor'] ) : ?>
126-
<?php
127-
if ( $attributes['showAvatar'] ) {
128-
echo get_avatar( get_the_author_meta( 'ID' ) );
129-
}
130-
?>
131-
<span class="byline">
133+
<?php if ( Newspack_Blocks::get_post_sponsors( get_the_id() ) ) : ?>
134+
<span class="sponsor-logos">
135+
<?php
136+
$logos = Newspack_Blocks::get_sponsor_logos( get_the_id() );
137+
foreach ( $logos as $logo ) {
138+
if ( '' !== $logo['url'] ) {
139+
echo '<a href="' . esc_url( $logo['url'] ) . '" target="_blank">';
140+
}
141+
echo '<img src="' . esc_url( $logo['src'] ) . '" width="' . esc_attr( $logo['width'] ) . '" height="' . esc_attr( $logo['height'] ) . '">';
142+
if ( '' !== $logo['url'] ) {
143+
echo '</a>';
144+
}
145+
}
146+
?>
147+
</span>
148+
<span class="byline sponsor-byline">
132149
<?php
133-
printf(
134-
/* translators: %s: post author. */
135-
esc_html_x( 'by %s', 'post author', 'newspack-blocks' ),
136-
'<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>'
137-
);
150+
$bylines = Newspack_Blocks::get_sponsor_byline( get_the_id() );
151+
echo esc_html( $bylines[0]['byline'] ) . ' ';
152+
foreach ( $bylines as $byline ) {
153+
echo '<span class="author"><a target="_blank" href="' . esc_url( $byline['url'] ) . '">' . esc_html( $byline['name'] ) . '</a></span>' . esc_html( $byline['sep'] );
154+
}
138155
?>
139-
</span><!-- .author-name -->
156+
</span>
140157
<?php
158+
else :
159+
if ( $attributes['showAuthor'] ) :
160+
if ( $attributes['showAvatar'] ) {
161+
echo get_avatar( get_the_author_meta( 'ID' ) );
162+
}
163+
?>
164+
<span class="byline">
165+
<?php
166+
printf(
167+
/* translators: %s: post author. */
168+
esc_html_x( 'by %s', 'post author', 'newspack-blocks' ),
169+
'<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>'
170+
);
171+
?>
172+
</span><!-- .author-name -->
173+
<?php
141174
endif;
142-
143-
if ( $attributes['showDate'] ) :
144-
printf(
145-
'<time class="entry-date published" datetime="%1$s">%2$s</time>',
146-
esc_attr( get_the_date( DATE_W3C ) ),
147-
esc_html( get_the_date() )
148-
);
149-
endif;
175+
endif;
176+
if ( $attributes['showDate'] ) :
177+
printf(
178+
'<time class="entry-date published" datetime="%1$s">%2$s</time>',
179+
esc_attr( get_the_date( DATE_W3C ) ),
180+
esc_html( get_the_date() )
181+
);
182+
endif;
150183
?>
151184
</div><!-- .entry-meta -->
152185
</div><!-- .entry-wrapper -->

0 commit comments

Comments
 (0)